# Comparing Eigenvectors, Mathematica vs. Matlab

I am trying to create the same outputs in Mathematica and Matlab, however I am running into trouble aligning the eigenvectors with the eigenvalues, I think the Matlab is doing something slightly more complex (Ordering/Sorting) than I anticipated.

using the example in on the Mathwork website, for [V,D] = eig(A). http://www.mathworks.co.uk/help/matlab/ref/eig.html#btifddh-2

TestMatrix = {{1, 2, 3}, {3, 1, 2}, {2, 3, 1}}

[V,D] = eig(A)

V =

-0.5774 + 0.0000i,   0.2887 - 0.5000i,   0.2887 + 0.5000i,

-0.5774 + 0.0000i,  -0.5774 + 0.0000i,  -0.5774 + 0.0000i,

-0.5774 + 0.0000i,   0.2887 + 0.5000i,   0.2887 - 0.5000i,

D =

6.0000 + 0.0000i,   0.0000 + 0.0000i,   0.0000 + 0.0000i,

0.0000 + 0.0000i,  -1.5000 + 0.8660i,   0.0000 + 0.0000i,

0.0000 + 0.0000i,   0.0000 + 0.0000i,  -1.5000 - 0.8660i,


However running this in Mathematica, I receive the following eigenvector

m = N[Eigensystem[TestMatrix]]

DV = DiagonalMatrix[m[]]

Vec=N[Eigenvectors[TestMatrix]]

Vec={

{1., 1., 1.},

{-0.5 - 0.866025 I, -0.5 + 0.866025 I, 1.},

{-0.5 + 0.866025 I, -0.5 - 0.866025 I, 1.}}


This is perfectly acceptable, I just now want to get both vectors into the same form. Note The Diagonal Matrix matches, however I seem unable to recreate V.

From reading the Matwork example, V is right eigenvectors. In this case the Form [V,D] = eig(A) returns matrix V, whose columns are the right eigenvectors of A such that AV = VD. & The eigenvectors in V are normalized so that the 2-norm of each is 1.

Trying to normalise Vec, now in mathematica, I am unable to recreate the results obtained in Matlab. Clearly I am doing something incorrectly, this is where I get a little lost. I have tried the '2-norm' function as described in Matlab, and also the Normalize function, the latter seems a little better but is still incorrect comparing to the results in the matlab example.

Could someone explain where I am going wrong with this, or simply how to recreate the same results in mathematica.

Here are my (...I assume...) incorrect attempts.

Firslty using Normalise.

TestMatrix = {{1, 2, 3}, {3, 1, 2}, {2, 3, 1}}

vec = N[Eigenvectors[TestMatrix]]

Table[Normalize[vec[[i]]], {i, 1, Length[vec]}]

{{0.57735, 0.57735, 0.57735},

{-0.288675 - 0.5 I, -0.288675 + 0.5 I,  0.57735 + 0. I},

{-0.288675 + 0.5 I, -0.288675 - 0.5 I,  0.57735 + 0. I}}


This quite close, to the Matlab output V, except the matrix seems to have been transposed, times by a negative, and some how the 2nd and 3rd eigenvalues have been switched (Comparing the Egienvalues and Vector columns) Clearly something has gone wrong, I probably shouldn't have used Normalise.
However my attempts using Norm(p,2) are nowhere near to the final resul.

I see another thread which touches on this topic but doesn't really help. https://stackoverflow.com/questions/5648975/matlab-vs-mathematica-eigenvectors

Can anyone out there explain this?

MATLAB always uses the LAPACK libraries to calculate eigenvectors which works on double precision, floating point numbers - MATLAB's default data type. Mathematica's method depends on its input type. For example, when you do

TestMatrix = {{1, 2, 3}, {3, 1, 2}, {2, 3, 1}}
Eigenvectors[TestMatrix]


You'll get an exact answer involving Sqrt and so on. Mathematica uses it's exact arithmetic methods to get this and they are different from LAPACK. When you do N[Eigenvectors[TestMatrix]] you are taking these exact results and approximating them as floating point numbers.

Now, if you use

TestMatrix = {{1., 2., 3.}, {3., 1., 2.}, {2., 3., 1.}}
Eigenvectors[TestMatrix]


Note the decimal point after each number which is telling Mathematica not to interpret these as exact integers but as floating point numbers. Since you've asked Mathematica to calculate the eigenvectors of a double precision floating point Matrix, it uses the same library as MATLAB - i.e. LAPACK.

This is what you get - matching MATLAB

{{-0.5773502691896255 + 0. I, -0.577350269189626 +
0. I, -0.5773502691896258 + 0. I}, {0.5773502691896261 +
0. I, -0.28867513459481314 -
0.4999999999999996 I, -0.2886751345948132 +
0.4999999999999998 I}, {0.5773502691896261 +
0. I, -0.28867513459481314 +
0.4999999999999996 I, -0.2886751345948132 - 0.4999999999999998 I}}

• Walking Randomly, Thanks for your help re: double precision. For a good few minutes I thought that you had solved my problem, but when I run this code in Mathematica I do not get the same answer as you?<hr/> // TestMatrix = {{1.0, 2.0, 3.0}, {3.0, 1.0, 2.0}, {2.0, 3.0, 1.0}}; // Eigenvectors[TestMatrix] // {{-0.57735, -0.57735, -0.57735}, {-0.288675 + 0.5 I, 0.57735 + 0. I, -0.288675 - 0.5 I}, {-0.288675 - 0.5 I, 0.57735 + 0. I, -0.288675 + 0.5 I}} Both of our Matrices also do not match the Matlab Example, the second column of the Matlab matrix is negative. Are you able to help? – Matthew Ward Jul 24 '14 at 18:44
• Or rather I should say that the second column of both of eigenvector Matrix is the negative of the Matlab matrix. & the order of the columns is slightly different (We actually have a different order too?), I cannot see the link here. I would have thought this would imply that our Eigenvalues should be different if the order of the eigenvector is different. But the eigenvalues calc matches the matlab example, I cannot see how these eigenvectors match? – Matthew Ward Jul 24 '14 at 18:53