[R] Inverse matrix using eigendecomposition

Berend Hasselman bhh at xs4all.nl
Tue Dec 13 06:28:53 CET 2011

wwreith wrote
> General goal: Write R code to find the inverse matrix of an nxn positive
> definite symmetric matrix. Use solve() to verify your code works.
> Started with a 3x3 matrix example to build the code, but something dosen't
> seem to be working. I just don't know where I am going wrong.
> ##Example matrix I found online
> A<-c(4,1,-1,1,2,1,-1,1,2)
> m<-matrix(A,nrow=3,ncol=3)
> ##Caculate the eigen vectors and eigenvalues
> E<-eigen(m, sym=TRUE)
> Q<-E$vectors
> V<-E$values
> n<-nrow(m)
> ##normalize the eigenvectors
> for(i in 1:n){
>   Q[,i]<-Q[,i]/sqrt(sum(Q[,i]^2))
> }
> ##verify dot product of vectors are orthogonal
> sum(Q[,1]*Q[,2])
> sum(Q[,1]*Q[,3])
> sum(Q[,2]*Q[,3])
> ##Begin creating QDQ^T matrix. Where Q are orthonormal eigenvectors, and D
> is a diagonal matrix with 1/eigenvalues on the diagonal. and Q^T is the
> transpose of Q. 
> R<-t(Q)
> D<-mat.or.vec(n,n)
> for(i in 1:n) {
>   D[i,i]<-1/V[i]
>   }
> P<-Q*D*R
> ## P should be the inverse of the matrix m. Check using 
> solve(m)
> ## solve(m) does not equal P? Any ideas of what I am missing/not
> understanding?

Homework questions are not answered.

But to give you a hint: look in the "An Introduction to R" manual Chapter 5
"Array and Matrices" especially section 5.7 "Matrix facilities".

You should be able to work out what's wrong in your script (a single


View this message in context: http://r.789695.n4.nabble.com/Inverse-matrix-using-eigendecomposition-tp4188673p4188885.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list