Finding Inverse of a Matrix in R Programming – inv() Function

inv() function in R Language is used to calculate inverse of a matrix.

Note: Determinant of the matrix must not be zero

Syntax: inv(x)

Parameters:
x: Matrix

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate 
# inverse of a matrix
  
# Loading library
library(matlib)
  
# Create 3 different vectors. 
a1 <- c(3, 2, 8
a2 <- c(6, 3, 2
a3 <- c(5, 2, 4
    
# Bind the 3 matrices row-wise  
# using the rbind() function. 
A <- rbind(a1, a2, a3) 
    
# find inverse using the inv() function. 
print(inv(A))

chevron_right


Output:

           [, 1]       [, 2]       [, 3]
[1, ] -0.2857143 -0.2857143  0.7142857
[2, ]  0.5000000  1.0000000 -1.5000000
[3, ]  0.1071429 -0.1428571  0.1071429

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate
# inverse of a matrix
  
# Loading Library
library(matlib)
  
# Creating a matrix
A = matrix(c(2, 5, 3, 4, 5, 2, 6, 3, 4), 3, 3
  
det(A)
  
# Calling inv() function
cat("Inverse of A:\n"
inv(A)

chevron_right


Output:

[1] -46
Inverse of A:
           [, 1]        [, 2]       [, 3]
[1, ] -0.3043478  0.08695652  0.3913044
[2, ]  0.2391304  0.21739130 -0.5217391
[3, ]  0.1086957 -0.17391304  0.2173913



My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.