R语言 矩阵逆运算
矩阵的逆是指矩阵的倒数,就像我们在普通算术中对一个数字的倒数一样,用来解方程以寻找未知变量的值。矩阵的逆数是指与原矩阵相乘后会得到一个相同的矩阵。
在处理线性代数表达式时,寻找矩阵的逆值是最常见的任务之一。我们只能找到那些正方形且行列式为非零的矩阵的逆数。
注意: 确保矩阵是非奇异的,即行列式不应该是0。
矩阵方程:
其中,
A^-1 是矩阵A的逆数。
x 是未知变量列。
B 是解矩阵。
矩阵的逆反应方程:
有两种方法可以找到矩阵的逆值。
- 使用solve()函数:
solve()是R语言中一个通用的内置函数,有助于解决以下线性代数方程,就像上面图片中显示的那样。它既可以应用于向量,也可以应用于矩阵。
# R program to find inverse of a Matrix
# Create 3 different vectors
# using combine method.
a1 <- c(3, 2, 5)
a2 <- c(2, 3, 2)
a3 <- c(5, 2, 4)
# bind the three vectors into a matrix
# using rbind() which is basically
# row-wise binding.
A <- rbind(a1, a2, a3)
# print the original matrix
print(A)
# Use the solve() function
# to calculate the inverse.
T1 <- solve(A)
# print the inverse of the matrix.
print(T1)
输出:
[,1] [,2] [,3]
a1 3 2 5
a2 2 3 2
a3 5 2 4
a1 a2 a3
[1,] -0.29629630 -0.07407407 0.4074074
[2,] -0.07407407 0.48148148 -0.1481481
[3,] 0.40740741 -0.14814815 -0.1851852
- 使用inv()函数:
inv()函数是R语言中的一个内置函数,特别用于查找矩阵的逆值。
注意:确保你已经在你的环境中安装了’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)
# determinant of matrix
print(det(A))
输出:
-28
寻找矩阵的反值:
# find inverse using the inv() function.
print(inv(t(A)))
输出:
[1,] -0.2857143 0.5 0.1071429
[2,] -0.2857143 1.0 -0.1428571
[3,] 0.7142857 -1.5 0.1071429