Matrix operation in R: I have a square matrix whose determinant is zero, i need to find its inverse in R programing. Is it possible ,if yes how? [closed]

A matrix with determinant 0 does not have an inverse but one can calculate a generalized inverse (also see Moore Penrose inverse) which is not a true inverse but may be useful depending on what you want to do. See the ginv function in the MASS package (which comes with R).

M <- matrix(1:9, 3)

det(M)
## [1] 0

solve(M)  # can't invert 
## Error in solve.default(M) : 
##   Lapack routine dgesv: system is exactly singular: U[3,3] = 0

library(MASS)
ginv(M)
##            [,1]          [,2]       [,3]
## [1,] -0.6388889 -5.555556e-02  0.5277778
## [2,] -0.1666667 -5.551115e-17  0.1666667
## [3,]  0.3055556  5.555556e-02 -0.1944444

Although M %*% ginv(M) is not the identity matrix ginv(M) is such that M %*% ginv(M) %*% M equals M .

all.equal(M %*% ginv(M) %*% M, M)
## [1] TRUE

Browse More Popular Posts

Leave a Comment