Does Matlab eig always returns sorted values?

kamaci picture kamaci · Dec 4, 2012 · Viewed 31.9k times · Source

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?

Answer

Rody Oldenhuis picture Rody Oldenhuis · Dec 4, 2012

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)