Does Matlab eig always returns sorted values?

試著忘記壹切 提交于 2019-12-28 02:56:05

问题


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?


回答1:


V is NOT sorted in any order, except to correspond to the order of the associated eigenvalues. But perhaps you did not mean that.

The eigenvalues TEND to be in descending order, but this is not assured at all. They tend to be in order because the largest tend to trickle out of the algorithm on top. Eig has no sort at the end to ensure that fact.

I might point out the eigenshuffle tool, designed to take a sequence of eigenproblems, then resorting the eigenvalues (and the corresponding eigenvectors) so they are consistent along the sequence.

If you really need them certainly in decreasing order, then do a sort to ensure that fact. Make sure you also sort the vectors in the same order.




回答2:


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)


来源:https://stackoverflow.com/questions/13704384/does-matlab-eig-always-returns-sorted-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!