How to check if a matrix is invertible or not in R?


If the matrix is singular then it is not invertible and if it is non−singular then it is invertible. Therefore, we can check if a matrix is singular or not. We can use is.singular.matrix function of matrixcalc for this purpose. For example, if we have a matrix called M then to check whether it is invertible or not, we can use is.singular.matrix(M).

Example1

Loading matrixcalc package and creating a matrix −

library(matrixcalc)
M1<−matrix(1:25,ncol=5)
M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1    6    11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25

Example

is.singular.matrix(M1)

Output

[1] TRUE

It means M1 is not invertible.

Example2

 Live Demo

M2<−matrix(rpois(64,8),nrow=8)
M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 4    3    9    9    7    8    6   1
[2,] 5    8    8    10  12    9    8   6
[3,] 6    6    7    14   9   11   13   8
[4,] 6    7    10   4   6    8     8   7
[5,] 13   8    10   6   3    7     9   4
[6,] 7    10   14   8   8    5    7    2
[7,] 3    9    7    9   6    4    6    8
[8,] 5    9    13   6   8    15   8    2

Example

is.singular.matrix(M2)

Output

[1] FALSE

It means M2 is invertible.

Example3

 Live Demo

M3<−matrix(rpois(36,5),nrow=6)
M3

Output

    [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    8    5    5     8    4
[2,] 4    9    4    2     4    6
[3,] 2    5    1    7     7    4
[4,] 4    4    1    7     5    2
[5,] 6    6    2    4     6   10
[6,] 9    6    1    4     9    3

Example

is.singular.matrix(M3)

Output

[1] FALSE

It means M3 is invertible.

Example4

 Live Demo

M4<−matrix(rnorm(36),nrow=6)
M4

Output

         [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] −0.2336251 0.3096045 −0.8818330 −0.04347818 −0.1778513 −0.4080015
[2,] −1.2177266 2.1817922 −0.9377145 −1.78284702 0.4995368 −1.3991093
[3,] 1.2766525 −0.2323218 0.8519997 0.31299613 −0.6803835 −1.1479453
[4,] 0.7903845 0.9700842 0.5117611 1.19601996 0.5250466 1.6161917
[5,] −1.3520358 0.6123879 1.9760799 −2.26366053 −1.4070510 0.8421128
[6,] 0.7777491 −2.0503730 −0.2613909 −1.35740539 0.9093589 0.9895680

Example

is.singular.matrix(M4)

Output

[1] FALSE

It means M4 is invertible.

Example5

 Live Demo

M5<−matrix(runif(25,2,5),nrow=5)
M5

Output

      [,1]    [,2]     [,3]     [,4]     [,5]
[1,] 4.576693 3.199907 4.005826 2.526450 3.443598
[2,] 2.470785 2.327514 2.169090 4.098793 3.940592
[3,] 4.547359 4.909557 3.066596 2.676077 3.837429
[4,] 4.135820 2.309655 3.839309 3.164276 3.802660
[5,] 2.634209 3.422963 3.619294 2.017092 3.601271

Example

is.singular.matrix(M5)

Output

[1] FALSE

It means M5 is invertible.

Example6

 Live Demo

M6<−matrix(rexp(25,1.25),nrow=5)
M6

Output

      [,1 ]          [,2]       [,3]       [,4]    [,5]
[1,] 0.1643510 0.1040981 0.584215338 2.2388616 0.05568353
[2,] 0.1125260 0.6645813 0.004011225 0.4228827 2.27699989
[3,] 0.7495866 1.3936340 0.823750284 0.9881828 0.51836239
[4,] 0.2237822 2.6784765 0.614246592 1.1034886 1.11131293
[5,] 3.9557367 1.4623781 0.594690357 0.4180981 0.70791124

Example

is.singular.matrix(M6)

Output

[1] FALSE

It means M6 is invertible.

Example7

 Live Demo

M7<−matrix(rpois(64,2),ncol=8)
M7

Output

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    2    2    1    1    2    2    2    1
[2,]    3    3    1    2    4    0    1    1
[3,]    0    3    1    3    2    2    1    2
[4,]    2    1    2    1    2    1    1    4
[5,]    2    3    0    2    0    2    1    1
[6,]    3    2    6    1    0    1    2    2
[7,]    4    1    3    4    2    2    3    4
[8,]    2    0    1    2    4    2    1    1

Example

is.singular.matrix(M7)

Output

[1] FALSE

It means M7 is invertible.

Example8

 Live Demo

M8<−matrix(rnorm(16,5,2.1),ncol=4)
M8

Output

         [,1]       [,2]    [,3]    [,4]
[1,] 4.058489 6.951464 4.846994 6.376624
[2,] 3.786127 10.430663 6.350964 4.439200
[3,] 4.166099 1.210901 7.712636 5.234337
[4,] 2.068379 4.461313 4.033786 3.732698

Example

is.singular.matrix(M8)

Output

[1] FALSE

It means M7 is invertible.

Updated on: 05-Feb-2021

735 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements