Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in R Programming by (7.3k points)
edited by

I was wondering what is your recommended way to compute the inverse of a matrix?

The ways I found seem not satisfactory. For example,

> c=rbind(c(1, -1/4), c(-1/4, 1))  

> c

      [,1]  [,2]

[1,]  1.00 -0.25

[2,] -0.25  1.00

> inv(c)

Error: could not find function "inv"  

> solve(c)

          [,1]      [,2]  

[1,] 1.0666667 0.2666667  

[2,] 0.2666667 1.0666667  

> solve(c)*c  

            [,1]        [,2]  

[1,]  1.06666667 -0.06666667  

[2,] -0.06666667  1.06666667  

> qr.solve(c)*c  

            [,1]        [,2]  

[1,]  1.06666667 -0.06666667  

[2,] -0.06666667  1.06666667  

Thanks!

1 Answer

0 votes
by
edited by

To get inverse of a matrix, you can use the solve function but with the matrix multiplication.i.e.,

c=rbind(c(1, -1/4), c(-1/4, 1))  

> c

      [,1]  [,2]

[1,]  1.00 -0.25

[2,] -0.25  1.00

solve(c)

          [,1]      [,2]

[1,] 1.0666667 0.2666667

[2,] 0.2666667 1.0666667

solve(c) %*% c

     [,1] [,2]

[1,]    1    0

[2,]    0    1

qr.solve(c) %*% c

     [,1]          [,2]

[1,]    1 -5.551115e-17

[2,]    0  1.000000e+00

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...