[R] Matrix eigenvectors in R and MatLab

David Brahm brahm at alum.mit.edu
Fri Apr 4 23:23:26 CEST 2003


Mikael Niva <mikael.niva at ebc.uu.se> wrote:
> Is there anyone who knows why I get different eigenvectors when I run
> MatLab and R?

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.
-- 
                              -- David Brahm (brahm at alum.mit.edu)



More information about the R-help mailing list