Skip to content

Matrix eigenvectors in R and MatLab

6 messages · Mikael Niva, David Brahm, Thomas W Blackwell +2 more

#
Dear R-listers

Is there anyone who knows why I get different eigenvectors when I run
MatLab and R? I run both programs in Windows Me. Can I make R to produce
the same vectors as MatLab?

#R Matrix
PA9900<-c(11/24 ,10/53 ,0/1 ,0/1 ,29/43 ,1/24 ,27/53 ,0/1 ,0/1 ,13/43
,14/24 ,178/53 ,146/244 ,17/23 ,15/43 ,2/24 ,4/53 ,0/1 ,2/23 ,2/43 ,4/24
,58/53 ,26/244 ,0/1 ,5/43)

#R-syntax
PA9900<-matrix(PA9900,nrow=5,byrow=T)
eigen(PA9900)

#R-output
$values
[1]  1.2352970  0.3901522 -0.2562860  0.2259411  0.1742592

$vectors
            [,1]        [,2]        [,3]      [,4]       [,5]
[1,] -0.67795430 -1.70686496 -0.52613955 -8.675109 -0.8413826
[2,] -0.32621100  0.54611272 -0.21526356 -2.726193 -0.2876643
[3,] -2.83313878 -2.88801964  0.87388189 45.427935  4.5069361
[4,] -0.09857565 -0.33015962  0.09136359 -5.426254 -0.8201206
[5,] -0.68977432  0.01977374  0.61772506  3.751978  0.4348802


%Matlab Matrix
PA9900 =[11/24 10/53 0/1 0/1 29/43 ;1/24 27/53 0/1 0/1 13/43 ;14/24
178/53 146/244 17/23 15/43 ;2/24 4/53 0/1 2/23 2/43 ;4/24 58/53 26/244
0/1 5/43]

%MatLab-syntax
[wmat,dmat]=eig(mat)

%MatLab-output
wmat =

   -0.2250    0.4330   -0.4998   -0.1795   -0.1854
   -0.1083    0.1771    0.1599   -0.0614   -0.0583
   -0.9403   -0.7191   -0.8457    0.9617    0.9708
   -0.0327   -0.0752   -0.0967   -0.1750   -0.1160
   -0.2289   -0.5083    0.0058    0.0928    0.0802


dmat =

    1.2353         0         0         0         0
         0   -0.2563         0         0         0
         0         0    0.3902         0         0
         0         0         0    0.1743         0
         0         0         0         0    0.2259

Yours sincerely, Mikael Niva

********************************************
Mikael Niva
Avd. f?r V?xtekologi, Dept. of Plant Ecology
EvolutionsBiologiskt Centrum, Uppsala Universitet
Villav?gen 14
752 36 UPPSALA
E-post Mikael.Niva at EBC.UU.SE
Tel. +46 (0)18 471 28 65
Fax +46 (0)18 55 34 19
1 day later
#
Mikael Niva <mikael.niva at ebc.uu.se> wrote:
R orders the eigenvalues by absolute value, which seems sensible; the MatLab
eigenvalues you gave do not seem to be in any particular order.

R does not normalize the eigenvectors (as MatLab does), but you can easily do
so yourself:

R> PA9900<-c(11/24 ,10/53 ,0/1 ,0/1 ,29/43 ,1/24 ,27/53 ,0/1 ,0/1 ,13/43
R>   ,14/24 ,178/53 ,146/244 ,17/23 ,15/43 ,2/24 ,4/53 ,0/1 ,2/23 ,2/43 ,4/24
R>   ,58/53 ,26/244 ,0/1 ,5/43)
R> PA9900<-matrix(PA9900,nrow=5,byrow=T)
R> eig <- eigen(PA9900)

R> eig$values   # Note they are in descending order of absolute value:
[1]  1.2352970  0.3901522 -0.2562860  0.2259411  0.1742592

R> sweep(eig$vectors, 2, sqrt(colSums(eig$vectors^2)), "/")
            [,1]         [,2]        [,3]        [,4]        [,5]
[1,] -0.22500913 -0.499825704 -0.43295788 -0.18537961 -0.17952679
[2,] -0.10826756  0.159919608 -0.17713941 -0.05825639 -0.06137926
[3,] -0.94030246 -0.845706299  0.71911349  0.97075584  0.96165016
[4,] -0.03271669 -0.096681499  0.07518268 -0.11595437 -0.17499009
[5,] -0.22893213  0.005790397  0.50832318  0.08017655  0.09279089


This is the same as the MatLab result you gave, except for 2 things:

1) The column order matches the eigenvalue order, so R's columns are in a
   different order than Matlab's.

2) The sign is different for one of the vectors (my column 3, your 2).  The
   sign of an eigenvector is not well defined, even after normalization.

MatLab> wmat =
MatLab>    -0.2250    0.4330   -0.4998   -0.1795   -0.1854
MatLab>    -0.1083    0.1771    0.1599   -0.0614   -0.0583
MatLab>    -0.9403   -0.7191   -0.8457    0.9617    0.9708
MatLab>    -0.0327   -0.0752   -0.0967   -0.1750   -0.1160
MatLab>    -0.2289   -0.5083    0.0058    0.0928    0.0802
MatLab> 
MatLab> dmat =
MatLab>     1.2353         0         0         0         0
MatLab>          0   -0.2563         0         0         0
MatLab>          0         0    0.3902         0         0
MatLab>          0         0         0    0.1743         0
MatLab>          0         0         0         0    0.2259

   Side note: there is some relation between eigenvectors and svd (singular
value decomposition) which I have not fully grokked yet; if anyone has a simple
explanation I'd be grateful.
#
Mikael  -

The matrix PA9900 is not a symmetric matrix.  Eigen() will
automatically detect this.  help("eigen") says explicitly:

"For `eigen( , symmetric = FALSE)' the choice of length of
the eigenvectors is not defined by LINPACK.  In all other
cases the vectors are normalized to unit length."

In the example you give, the eigenvectors from R are clearly
NOT normalized to unit length, while those from Matlab are.
Even after normalizing them, the R eigenvectors will differ
by order and sign from the Matlab ones.  (Compare R column 3
with Matlab column 2.)  Have to look at the EISPACK source
documentation to see whether it's returning right eigenvectors
or left eigenvectors for an asymmetric matix.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Thu, 3 Apr 2003, Mikael Niva wrote:

            
#
Excellent analysis, Thomas.  An alternative to looking at EISPACK 
documentation is to do the following computations:

	(1) Values %*% diag(vectors) %*% solve(Values)

	(2) solve(Values) %*% diag(vectors) %*% Values

One of these two should return the original matrix; the other will 
likely be very different.  If so, the mystery is solved.  If (1) returns
PA9900, then

	PA9900 %*% Values = Values %*% diag(vectors)

Else

	Values %*% PA9900 = diag(vectors) %*% Values

Best Wishes,
Spencer Graves
Thomas W Blackwell wrote:
3 days later
#
Regarding the relationship between eigen and svd:

	  For symmetric matrices, the svd is a solution to the Eigenvalue 
problem.  However, if eigenvectors are not normalized to length 1, then 
the two solutions will not look the same.

	  Another current question asked about the differences in eigenanalysis 
between R and Matlab.  In sum, it appears that R sorts the eigenvalues 
in decreasing order of absolute values while Matlab does not, but Matlab 
normalizes the eigenvectors to length 1 while R does not.

Spencer Graves
David Brahm wrote:
#
On Tue, 8 Apr 2003, Spencer Graves wrote:

            
The last is not wholly accurate for R 1.6.2 (it only applies to
eigen(symmetric=FALSE)), and the imminent R 1.7.0 will normalize
the eigenvectors (except in back-compatibility mode).