# Projecting data points onto eigenvector space

26 views (last 30 days)
Hello,
I am trying to code a principal component analysis (PCA) on a dataset (8 samples , 2 features) and I can not plot the datapoints' projections on the eigenvector which provide the largest variace (eigenvector of the 1st principal component). The code is as following:
x=[1 1 2 0 5 4 5 3; 3 2 3 3 4 5 5 4]';
X=mean(x);
m=mean(x')';
x_m=x-X;
D=cov(x_m)
[eigenVector,lamda]=eig(D);
lamdasort=sort(lamda);
w2=eigenVector(:,2)'.*x;
robustness=lamda(2,2)/(lamda(1,1)+lamda(2,2))
figure(1)
hold on
scatter(x(:,1),x(:,2),'o')
scatter(x(:,1),x(:,2),'.k')
plot(X(1,1),X(1,2),'.g')
xlabel('x1')
ylabel('x2')
xlim([-2 6])
ylim([-2 6])
figure(2)
hold on
scatter(x(:,1),x(:,2),'o')
scatter(w2(:,1),w2(:,2),'.k')
So I would like w2 to be the projections of the data set (hence eigenVector(:,2)*x) to the eigenvector of the highest-value eigenvalue. I think smth is wrong with this approach, I get somthing like inverse of the dataset (figure (2)). I multiply the k=1 dimension (eigenvector) with the dataset (w2=eigenVector(:,2)'.*x;).
Thank you
Edit: This is the result that I cannot code
This is what i get when multiplying the eigenvector with the dataset.

Christine Tobler on 28 Jan 2021
The lambda here is a diagonal matrix, so SORT will sort each of its columns, not the eigenvalues on the diagonal among themselves. Also, after sorting the eigenvalues, make sure that you also permute the eigenvectors in the same way:
[eigenVector,lamda]=eig(D, 'vector'); % returns eigenvalues as a column vector lambda
[lamdasort,ind]=sort(lamda);
eigenVector = eigenVector(:, ind);
Konstantinos Bampilis on 28 Jan 2021
I used the largest eigenvalue, and the corresponding eigenvector.