Search code examples
pythonsortingeigenvector

Sorting eigen values and corresponding eigen vectors


For a matrix A, I have a set of eigenvalues and corresponding eigenvectors, obtained using the standard method, eigvals, eigvecs = la.eig(A) after importing scipy.linalg as la.
The eigenvalues are sorted using np.sort(eigvals, axis=-0).
How do we rearrange the corresponding eigenvectors.


Solution

  • I believe what you need is to get the list of sorted indexes, then take eigenvectors and eigenvalues in that order using np.argsort.

    It would be something like:

    import numpy as np
    import scipy.linalg as la
    
    A =  np.random.rand(3, 3)
    eigvals, eigvecs = la.eig(A)
    sorted_indexes = np.argsort(eigvals)
    eigvals = eigvals[sorted_indexes]
    eigvecs = eigvecs[:,sorted_indexes]
    

    P.S.: seems @srikrishna already posted a link with the solution. I will maintain the response stays in Stackoverflow.