Reputation: 5420
I'm trying to plot a the eigenvectors of a 2D Dataset, for that I'm trying to use the quiver
function in Matlab, here's what I've done so far :
% 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))
My problem is the last line, I get the following error :
Error using quiver (line 44)
The size of Y must match the size of U or the number of rows of U.
It seems that I'm missing a size here but I can't figure out where! thanks in advance for any hint
Upvotes: 0
Views: 1287
Reputation: 23675
As the error says, X
and Y
parameters must have, respectively, the same size of U
and V
parameters. If you change the last part of your code:
% ploting the eigenvectors !
hold on
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1))
as follows:
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;
your script should properly work.
Upvotes: 1