Different eigenvectors calculated from Matlab and Python

62 views (last 30 days)
Ruihong Jiang
Ruihong Jiang on 25 Dec 2021
Commented: Ruihong Jiang on 27 Dec 2021
I was studying an eigenvector problem and found Matlab and Python produce different eigenvectors for the same matrix. (The matrix does not have unique eigenvectors) R has the same result with Python. I was wondering the reason of this issue. Thank you!
An example of the matrix is
  1 Comment
Star Strider
Star Strider on 25 Dec 2021
Calculating them manually will also produce different eigenvectors. Eigenvectors are not unique.

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 25 Dec 2021
Edited: John D'Errico on 25 Dec 2021
Look at it this way. THERE IS NO ISSUE. While you say that R and Python produce the same result, does it matter? (It possibly means that R and Python may both use the same external tool to compute the eigenvalues and eigenvectors.)
You say yourself that the eigenvalues are not unique, because of the eigenvalue of multiplicity 2. But that means MATLAB, or ANY code, could arbitrarily have made a different choice.
A = [1 .4 .4; .4 1 -.4; .4 -.4 1]
A = 3×3
1.0000 0.4000 0.4000 0.4000 1.0000 -0.4000 0.4000 -0.4000 1.0000
[V,D] = eig(A)
V = 3×3
-0.5774 -0.1543 0.8018 0.5774 -0.7715 0.2673 0.5774 0.6172 0.5345
D = 3×3
0.2000 0 0 0 1.4000 0 0 0 1.4000
The second and third columns are the eigenvectors that correspond to the replicated eigenvalues. Are there different eigenvectors possible? Of course. Rotate them any way you wish.
V23 = V(:,2:3)
V23 = 3×2
-0.1543 0.8018 -0.7715 0.2673 0.6172 0.5345
lambda = D(2,2)
lambda = 1.4000
format long g
A*V23/1.4
ans = 3×2
-0.154303349962092 0.801783725737273 -0.771516749810459 0.267261241912424 0.617213399848368 0.534522483824849
As you can see, we get back V23 by this operation. But suppose I rotate the vectors? For example...
rot = @(theta) [cos(theta) sin(theta);-sin(theta) cos(theta)];
V23hat = V23*rot(pi/4)
V23hat = 3×2
-0.676055654631837 0.457837764395845 -0.734526962094594 -0.356562489085367 0.0584713074627576 0.814400253481212
So what you see are a completely different eigenvector pair. But they are still validly eigenvectors.
A*V23hat/lambda
ans = 3×2
-0.676055654631837 0.457837764395845 -0.734526962094594 -0.356562489085367 0.0584713074627575 0.814400253481212
As you can see, they still work nicely as eigenvectors for that eigenvalue.
Again. There is no issue. The only issue lies in your appreciation of what it means to be non-unique.
  1 Comment
Ruihong Jiang
Ruihong Jiang on 27 Dec 2021
Thank you John! I'm curious about what external tools Matlab/Python/R uses for calculating the eigenvectors.

Sign in to comment.


Matt J
Matt J on 25 Dec 2021
Edited: Matt J on 25 Dec 2021
Eigenvectors are not unique.
Especially in a symmetric matrix with eigenvalues having multiplicity >1, as is the case here,
eig([1.0000 0.4000 0.4000
0.4000 1.0000 -0.4000
0.4000 -0.4000 1.0000])
ans = 3×1
0.2000 1.4000 1.4000
Because this matrix is symmetric, the eigenvalue λ=1.4 is guaranteed to have at least two linearly independent eigenvectors. Moreover, every linear combination of those eigenvectors is also an eigenvector for λ=1.4.

Community Treasure Hunt

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

Start Hunting!

Translated by