Search code examples
matrixoctaveeigenvalue

How label the complex eigenvalues of a matrix in ascending order of their imaginary part in Octave?


When the function eig(a) is used, Octave does not seem to order complex eigenvalues in any particular order (while for real eigenvalues, Octave stores the eigenvalues and their eigenvectors in ascending order).

I need to order the complex eigenvalues (and also their respective eigenvectors) of a matrix in ascending order of their imaginary part. Is there any built in routine to do that?

If not, how to implement this?


Solution

  • The sort function has a 2nd output argument that gives the indices into the input in sorted order. For your case, if d is the vector of eigenvalues:

    [~,I] = sort(imag(d));
    d = d(I);
    

    You can use the same I to sort the corresponding eigenvectors V in the same order:

    V = V(:,I);