How to sort eigenvectors from lowest to highest corresponding eigenvalue?

I’m solving a Sturm-Liouville problem numerically, and in order to use the eigenvectors and eigenvalues I get out of it, it would be handy to be able to order the eigenvalues in order of ascending absolute value, and simultaneously reorder the eigenvectors appropriately so that they match up. So, say we’re dealing with eigenvalues and eigenvectors of H then if:

EIG=eigen(H);
Y=EIG.vectors;
Lam=EIG.values;

then, after the desired reordering, Lam[1] would be the eigenvalue of smallest absolute value and Y[:,1] would be its corresponding eigenvector.

If I am somehow unclear then I’ll explain what I’d do to accomplish this in MATLAB, I’d run, assuming [Y, Lam]=eig(H); Lam=diag(Lam);:

[Lam, IX]=sort(Lam, 'ascend');
Y=Y(:,IX);

. Is there a way to achieve this in Julia? I know that Lam=sort(Lam) would reorder Lam’s eigenvalues in ascending order of value, but it would not also reorder the eigenvectors appropriately.

sort and sortperm should… sort you out.

2 Likes
Y = Y[sortperm(Lam)]
1 Like

Small correction:

Y = Y[:, sortperm(Lam)]
2 Likes