在 Matlab 中绘制特征向量
plotting eigenvector in Matlab
我正在尝试绘制 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.
我这里好像少了一个尺码,但我不知道在哪里!
提前感谢任何提示
如错误所述,X
和 Y
参数必须分别具有与 U
和 V
参数相同的大小。如果您更改代码的最后一部分:
% 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;
您的脚本应该可以正常工作。
我正在尝试绘制 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.
我这里好像少了一个尺码,但我不知道在哪里! 提前感谢任何提示
如错误所述,X
和 Y
参数必须分别具有与 U
和 V
参数相同的大小。如果您更改代码的最后一部分:
% 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;
您的脚本应该可以正常工作。