Optimizing calculation of eigenvectors and eigenvalues

조회 수: 8 (최근 30일)
Giuseppe Cianci
Giuseppe Cianci 2018년 1월 18일
편집: Matt J 2021년 1월 16일
I have a quadratic matrix A with a size of about 2000x2000. I want to calculate its eigenvectors and eigenvalues. The eigenvalues must be in a vector (not a diagonal matrix as usual). I use the following code:
eigenvals = abs(real(eig(A)));
[eigenvecs, ~] = eig(A);
eigenvecs = real(eigenvecs);
The problem is that it takes a lot of time (about 15 seconds) and I need to repeat this process a lot of times in a loop. So I tried to optimize it and changed it to this:
[eigenvecs, eigenvals]=eig(A);
eigenvecs=real(eigenvecs);
eigenvals=abs(real(diag(eigenvals)));
This runs about 5 seconds faster. The calculated eigenvectors are the same for both code snippets, however the eigenvalues are not and differ a bit. How could I further optimize the code in a way that it delivers equivalent results to the first code snippet?
Thank you very much in advance!

채택된 답변

David Goodmanson
David Goodmanson 2021년 1월 16일
편집: David Goodmanson 2021년 1월 16일
Hi Guiseppi,
there is nothing guaranteed about the order of the eigenvalues that are produced by eig. And if you do the first method, which is basically
lambda = eig(A)
% and
[v, ~] = eig(A)
the order of eigenvalues may not be the same in each case, meaning that the order of eigenvalues and the order of rows of the eigenvector matrix may not match up. In fact for some examples I tried, they do not match up, which is similar to your experience. This means the first method does not work.
If you would like a vector rather than a diagonal matrix for lambda, there is the option
[v lambda] = eig(A,'vector')
which only calls eig once and so is faster than the first method, in addition to being correct.
  댓글 수: 1
Matt J
Matt J 2021년 1월 16일
편집: Matt J 2021년 1월 16일
which only calls eig once and so is faster than the first method
Also, with
[v, ~] = eig(A)
a 2000x2000 matrix memory allocation for the second output argument is still executed by eig, even though it is promptly discarded in the caller workspace because of '~'. Using the form eig(A,'vector') avoids that extra memory allocation step.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by