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?
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 .
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.
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.
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 .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With