[R] Inverse matrix using eigendecomposition

wwreith reith_william at bah.com
Tue Dec 13 04:53:59 CET 2011


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?

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



More information about the R-help mailing list