Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Matlab eig always returns sorted values?

I use a function at Matlab:

[V,D] = eig(C);

I see that V and D are always sorted ascending order. Does it always like that or should I sort them after I get V and D values?

like image 268
kamaci Avatar asked Dec 04 '12 13:12

kamaci


People also ask

Does EIG sort Matlab?

By default eig does not always return the eigenvalues and eigenvectors in sorted order. Use the sort function to put the eigenvalues in ascending order and reorder the corresponding eigenvectors. Calculate the eigenvalues and eigenvectors of a 5-by-5 magic square matrix. The eigenvalues of A are on the diagonal of D .

What is the difference between EIG and eigs in Matlab?

The eig() function calculates wrong eigenvalues with the default algorithm (Cholesky) and the right eigenvalues with the QZ-algorithm (Due to the condition?) The eigs() function calculates the right eigenvalues if the number of requested eigenvalues is and the wrong eigenvalues if.

Are eigenvalues sorted?

If the eigenvalues are complex, the sort order is lexicographic (that is, complex numbers are sorted according to their real part first, with ties broken by their imaginary part). Incidentally, it's more common to sort from largest to smallest eigenvalue.

How do you normalize eigenvectors in Matlab?

The form and normalization of W depends on the combination of input arguments: [V,D,W] = eig(A) returns matrix W , whose columns are the left eigenvectors of A such that W'*A = D*W' . The eigenvectors in W are normalized so that the 2-norm of each is 1. If A is symmetric, then W is the same as V .


1 Answers

If you want to guarantee sorted-ascending values, just do an extra

if ~issorted(diag(D))
    [V,D] = eig(A);
    [D,I] = sort(diag(D));
    V = V(:, I);
end

to sort them the way you want.

Alternatively, use eigs:

[V,D] = eigs(A,size(A,1)-1)
like image 139
Rody Oldenhuis Avatar answered Sep 21 '22 06:09

Rody Oldenhuis