rmatrix-inverse

How to check if a matrix has an inverse in the R language


How do you determine if a matrix has an inverse in R?

So is there in R a function that with a matrix input, will return somethin like:

"TRUE" (this matrix has inverse)/"FALSE"(it hasn't ...).


Solution

  • @MAB has a good point. This uses solve(...) to decide if the matrix is invertible.

    f <- function(m) class(try(solve(m),silent=T))=="matrix"
    x <- matrix(rep(1,25),nc=5)          # singular
    y <- matrix(1+1e-10*rnorm(25),nc=5)  # very nearly singular matrix
    z <- 0.001*diag(1,5)                 # non-singular, but very smalll determinant
    f(x)
    # [1] FALSE
    f(y)
    # [1] TRUE
    f(z)
    # [1] TRUE