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);

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);    
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.

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?


1 Answer 1


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

Remark about computation efficiency: As you sort the eigenvalues, you could have obtained the corresponding permutation indices.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.