Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort eigenvalue matrix with eigenvector matrix

I have N eigenvalues in column vector form. Thus there are N eigenvectors corresponding to these eigenvalues, forming an eigenvector matrix.

Now, the problem I am working on requires me to sort the eigenvalues column vector in descending order. How do I sort the eigenvectors matrix in the same order as their eigenvalues in order to preserve correspondence?

like image 434
Bonk Avatar asked Jul 05 '11 21:07

Bonk


3 Answers

For example,

m = RandomReal[{0, 1}, {5, 5}];
{evals, evecs} = Eigensystem[m];
SortBy[Transpose[{evals, evecs}], First]

or if you want them in the same form, replace the last line by

Transpose@SortBy[Transpose[{evals, evecs}], First]

EDIT: while I used {evals,evecs}=Eigensystem[m], that's not necessary. I could just have used s=Eigensystem[m] and then used s wherever I currently have {evals,evecs}.

like image 145
acl Avatar answered Oct 09 '22 18:10

acl


While @acl and @yoda's ways of sorting (i.e. pairing the list elements then sorting together) is easy and commonly used, I'd like to show another generic method to easily sort an arbitrary number of lists based on one particular list (list1):

oo = Ordering[list1]; (* this finds the sorting order of list1 *)
list1[[oo]]
list2[[oo]]
list3[[oo]]  (* these order some other lists in the same way *)
like image 36
Szabolcs Avatar answered Oct 09 '22 19:10

Szabolcs


You can use the Sort function to sort the eigensystem according to the eigenvalues.

mat = (#*Transpose@#) &@RandomReal[NormalDistribution[], {4, 4}];
eigsys = Sort@Transpose@Eigensystem[mat];

Sort's default behavior is to sort by the first column.

like image 45
abcd Avatar answered Oct 09 '22 19:10

abcd