# Reducing features to 2 by PCA confusion Matlab

I am trying to reduce $$500$$ features to $$2$$ as an assignment. I wrote the following code and I am deeply concerned if it is true as when I plot it on the graph it does not look good. It should look good as I have to apply cluster algorithm to is as a next step. Can you help me pls?

X = data;

X = bsxfun(@minus,X,mean(X));  %%standardize

CM = transpose(X)*X;  %%coveriance matrix is calculated

[V,D] = eig(CM);
eigenvalues = zeros(1,306);

for k = 1:306
eigenvalues(1,k) = D(k,k);
end

sortedEigenValues = sort(eigenvalues,'descend');  %%eigenvalues are sorted
sortedEigenVectors = zeros(306,306);

for k=1:306  %%eigenvectors are sorted according to eigenvalues
for l=1:306
if sortedEigenValues(1,k) == D(l,l)
sortedEigenVectors(:,k) = V(:,l);
end
end
end
projectionMatrix = sortedEigenVectors(:,1:2); %% first to columns of sortedEigenVectors matrix are taken to create projection matrix
projectedMatrix = CM*projectionMatrix; %%projectedMatrix is what I am intending to use.

figure
stem(projectedMatrix(:,1),projectedMatrix(:,2)); %%plotting altogether


I have found eigenvalues and sort them and also sort eigenvectors and I take first $$2$$ eigenvectors and multiply it with the coveriance matrix and hope that this new matrix is what I am supposed to cluster, but I am very confused. Can you give some feedback?

After obtaining the principal component, you should compute $$XW$$ rather than $$X^TXW$$ where $$W$$ is the principal component.