R语言 查找矩阵的幂
在这篇文章中,我们将看到如何在R编程语言中计算矩阵的幂。矩阵是将数字排列成行和列。
在R编程中寻找矩阵幂的不同方法
- 通过使用%^%。
- 通过使用一个幂函数。
方法1:通过使用%^%
在使用这个方法之前,我们需要将expm库导入我们的R studio中。
将expm库安装到R studio 中
第1步: 首先你需要选择工具。
第2步: 选择工具后,你需要按安装包。
下面是实现的过程
我们通过使用矩阵函数导入expm并将数值分配到mat中。之后,我们要找到矩阵的功率。
# loading expm library
library(expm)
# creating mat variable and storing
# matrix into it.
mat <- matrix(1:9, nrow=3)
# In matrix function data will fill column wise,
# here data will be 1 to 9 as we mentioned 1:9
# and rows will be 3 as we given nrows as 3
# finding power of matrix here power is 4
mat %^% 4
输出
方法2:通过使用一个幂函数
为了使用幂函数,我们需要在我们的Rstudio中安装matrixcalc包。
下面是实现方法
在这里,我们导入matrixcalc,并通过使用矩阵函数将数值分配到矩阵中。之后,我们通过使用幂函数来寻找矩阵的幂。
# loading package
require(matrixcalc)
# creating matrix.
a<- matrix(1 : 9, nrow = 3)
# finding power of matrix by using power function.
# In matrix function data will fill column wise,
# here data will be 1 to 9 as we mentioned 1:9
# and rows will be 3 as we given nrows as 3
matrix.power(a, 9)
输出