2013-02-23 96 views
0

Matlab新手。 创建脚本以生成2D分布并确定其主要组件。绘制2D主分量

我绘制主要组件的方法是曲折的。必须有一个更优雅的方式来绘制主要组件。它是什么?

我可以在我的线段末端获得箭头吗? 这是我的.m文件:

%Distrubtion variances 
xx = 2; 
yy = .6; 
xy = .5; 

%Create distribution data 
A = mvnrnd([0 0] , [xx xy; xy yy], 100); 

%Get principal components of data 
coeff = pca(A); 

%Plot distribution 
h = plot(A(:,1),A(:,2),'b.'); 
hold on 

%Do crazy stuff to plot principal components 
temp=zeros(2,4); 
temp(:, 2:2:end) = coeff; 
scalefactor=10; %Make the lines longer 
temp=scalefactor * temp 
X=temp(1:2:end); 
Y=temp(2:2:end); 
plot(X, Y, 'r', 'LineWidth', 2); 

%format plot 
axis('square') 
grid on; 
xlabel('X Axis'); 
ylabel('Y Axis'); 
xlim([-10, 10]); 
ylim([-10 10]) 

shg; 
hold off; 

回答

1

这可能更合理。函数quiver绘制箭头。 (See docs)。你仍然需要自己调整箭头的大小。也许计算你的数据的范围而不是10,并在缩放因子和轴限制中使用它。

n = length(coeff); 
coeff = 10 * coeff; % Make the lines longer 
quiver(zeros(1,n), zeros(1, n), coeff(1,:), coeff(2,:), 'r', 'LineWidth', 2); 

另外,我不知道你用的是什么版本的MATLAB的,但pca是我的版本princomp