Why do x<-b%*%ginv(A) and x<-solve(A,b) give different results?. It seems that I am missing some basic feature of matrix indexing. e.g.: A<-matrix(c(0,-4,4,0),nrow=2,ncol=2) b<-c(-16,0) x<-b%*%ginv(A);x x<-solve(A,b);x Thanks in advance, Angel
Angel wrote:> Why do > x<-b%*%ginv(A)Why should it be the same??? Here, you are calculating (A+ is G-Inverse) x = b * A+> and > x<-solve(A,b)Here, you are calculating (A- is Inverse of A) A * x = b <=> A- * A * x = A- * b <=> x = A- * b So you have to compare, e.g.: ginv(A) %*% b solve(A, b) Uwe Ligges> give different results?. It seems that I am missing some basic feature of > matrix indexing. > e.g.: > > A<-matrix(c(0,-4,4,0),nrow=2,ncol=2) > b<-c(-16,0) > x<-b%*%ginv(A);x > x<-solve(A,b);x > > Thanks in advance, > Angel > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> Why do > x<-b%*%ginv(A) > and > x<-solve(A,b) > give different results?.they do (in cases the solution to A x = b is unique): R> A <- matrix(c(0,-4,4,0),nrow=2,ncol=2) R> A [,1] [,2] [1,] 0 4 [2,] -4 0 R> b <- c(-16,0) R> x1 <- ginv(A) %*% b <- NOT b %*% ginv(A) R> x1 [,1] [1,] 0 [2,] -4 R> x2 <- solve(A) %*% b R> x2 [,1] [1,] 0 [2,] -4 R> x3 <- solve(A, b) R> x3 [1] 0 -4 Torsten> It seems that I am missing some basic feature of > matrix indexing. > e.g.: > > A<-matrix(c(0,-4,4,0),nrow=2,ncol=2) > b<-c(-16,0) > x<-b%*%ginv(A);x > x<-solve(A,b);x > > Thanks in advance, > Angel > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
"Angel" <angel_lul at hotmail.com> writes:> Why do > x<-b%*%ginv(A) > and > x<-solve(A,b) > give different results?. It seems that I am missing some basic feature of > matrix indexing. > e.g.: > > A<-matrix(c(0,-4,4,0),nrow=2,ncol=2) > b<-c(-16,0) > x<-b%*%ginv(A);x > x<-solve(A,b);x[ginv() is from MASS, please remember to tell us] Your b is on the wrong side, try ginv(A)%*%b (possibly put it within drop() to convert it to vector). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907