Search code examples
matlaboctavepcasparse-matrix

Octave/Matlab: PCA on sparse matrix: how to get only the most important eigenvectors?


I am using Octave and have a huge sparse matrix that I have to get the eigenvalues of. However, if I just use a function to get all eigenvalues and eigenvectors, the result will take up way too much space, since the input matrix is sparse for a reason.

How can I get only a limited number of the most important eigenvectors?


Solution

  • Use eigs instead of eig:

    D = eigs(A,k);
    

    This returns the k largest eigenvalues of the matrix A. According to this page, Octave does support eigs for sparse matrices. eigs uses different techniques than eig, is slower overall, and shouldn't generally be used except in the cases such as the one you describe.

    Be sure to check out the options for the sigma argument in case you want the largest eigenvalues with respect to their real parts only, for example.

    The Matlab documentation for eigs is here.