pythonmatlabmatrixlinear-algebraeigenvector

Different eigenvectors from Python and Matlab


I need your help please with this... I'm trying to compute the eigenvectors of this matrix:

0.683043    -0.0248989  0.291915    0.0220227   -0.350487   -0.318394
0.435584    0.276779    0.192531    -0.00158314 -0.653018   -0.182419
0.409294    -0.196908   0.321927    0.409339    -0.214044   -0.301017
1               0           0           0            0           0
0               1           0           0            0           0
0               0           1           0            0           0

What I have in Matlab is [V,D] = eig([A; eye(d*(na-1),d*na)]) and I get this:

Matlab result

while using Python with this np.linalg.eig(np.block([[A], [np.eye((na-1)*d, na*d)]])), I get this:

Python result

Values are almost the same, but the last two columns have excatly opposite signs :( is there a way to get exactly the same output? I have been seeing the documentation but it has not been possible.

I tried using different hyperparameters and the scipy function, but it continues different


Solution

  • Exactly the opposite sign just means the vector is the opposite direction, i.e. correct. An eigenvector tells you the direction of a new axis to describe the matrix (PCA has good intuition on this), so being negative is correct.

    Example: in cartesian coordinates, which of the three vectors following define the x axis: [1,0], [-1,0], [0,1]. Answer: both [1, 0] and [-1, 0] define the x axis

    Is there a way to get the same values? No, it depends on the implementation of how eigenvectors are computed, so you can't force one or the other. Make whatever code you use to compare/validate them be flexible to this. Note there are also cases (depends on the data) where they can be in a different order