Search code examples
matlabimage-processingdimensionality-reduction

How do i obtain only the first principal component in MATLAB?


For certain measurements i need to obtain only the numeric value of the first principal component from the matrix. Can someone please tell me how do i go about it?


Solution

  • the most straight forward way is just to get the top eigenvector/value of your data's covariance matrix using eigs

    say the data matrix x is N by D, or # of data by dimension of data

    you can simply do

    C = cov(X);
    [V, D] = eigs(C, 1);
    

    in fact, you can get the top k principal components by running

    [V, D] = eigs(C, k);