【问题标题】:plotting eigenvector in Matlab在 Matlab 中绘制特征向量
【发布时间】:2017-12-24 23:28:44
【问题描述】:

我正在尝试绘制 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】:

    正如错误所说,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;
    

    您的脚本应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2011-07-03
      • 1970-01-01
      相关资源
      最近更新 更多