在Matlab中绘制特征向量

问题描述 投票:0回答:1

我正在尝试绘制2D数据集的特征向量,因为我正在尝试在Matlab中使用quiver函数,这是我到目前为止所做的:

    % generating  2D data 
clear ;
s  = [2 2] 
set = randn(200,1);
x = normrnd(s(1).*set,1)+3
y = normrnd(s(1).*set,1)+2
x_0 = mean(x)
y_0 = mean (y) 
c = linspace(1,100,length(x)); % color

scatter(x,y,100,c,'filled')
xlabel('1st Feature : x')
ylabel('2nd Feature : y')
title('2D dataset')
grid on
% gettign the covariance matrix 
covariance = cov([x,y])
% getting the eigenvalues and the  eigenwert 
[eigen_vector, eigen_values] = eig(covariance) 
eigen_value_1 = eigen_values(1,1) 
eigen_vector_1 =eigen_vector(:,1)
eigen_value_2 = eigen_values(2,2) 
eigen_vector_2 =eigen_vector(:,2)

% ploting the eigenvectors ! 
hold on 
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1))

我的问题是最后一行,我收到以下错误:

    Error using quiver (line 44)
The size of Y must match the size of U or the number of rows of U.

看来我在这里错过了一个大小,但我无法弄清楚在哪里!提前感谢任何提示

matlab pca
1个回答
1
投票

正如错误所说,XY参数必须分别具有相同大小的UV参数。如果您更改代码的最后部分:

% ploting the eigenvectors ! 
hold on 
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1))

如下:

x_0 = repmat(x_0,size(eigen_vector_2,1),1);
y_0 = repmat(x_0,size(eigen_vector_1,1),1);

% ploting the eigenvectors ! 
hold on;
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1));
hold off;

你的脚本应该正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.