如何在R中求矩阵的幂?
在R中不能直接求矩阵的幂,因为基础R中没有这样的函数。为此,我们可以使用expm包的%^%运算符。首先,我们将安装expm包,然后加载它并使用%^%。例如,假设我们有一个名为M的矩阵,我们想求M的平方,则可以这样操作:M %^% 2
示例
安装和加载expm包:
install.packages("expm") library(expm)
示例
M1<-matrix(1:25,nrow=5) M1
输出
[,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
示例
PowerM1<-M1%^%2 PowerM1
输出
[,1] [,2] [,3] [,4] [,5] [1,] 215 490 765 1040 1315 [2,] 230 530 830 1130 1430 [3,] 245 570 895 1220 1545 [4,] 260 610 960 1310 1660 [5,] 275 650 1025 1400 1775
示例
M2<-matrix(sample(1:10,36,replace=TRUE),nrow=6) M2
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 1 5 4 6 8 [2,] 7 1 9 2 3 1 [3,] 6 8 4 9 3 6 [4,] 6 3 7 6 9 10 [5,] 7 1 5 5 6 8 [6,] 3 9 4 10 3 10
示例
PowerM2<-M2%^%5 PowerM2
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 5677617 4739628 6065058 7081674 5548641 8398595 [2,] 4479295 3740518 4785348 5588349 4377963 6627882 [3,] 7079665 5917035 7556600 8838028 6916227 10476843 [4,] 8396194 7011395 8967480 10474806 8204430 12421001 [5,] 6592060 5505091 7039821 8224081 6440718 9751106 [6,] 7884913 6587253 8417738 9840903 7704426 11668026
示例
M3<-matrix(sample(1:50,36),ncol=6) M3
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 10 4 45 19 17 38 [2,] 29 46 28 27 41 40 [3,] 33 13 34 25 32 44 [4,] 2 16 7 39 23 26 [5,] 18 3 6 36 20 21 [6,] 8 12 47 24 48 49
示例
PowerM3<-M3%^%2 PowerM3
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 2349 1620 4113 3688 4375 5233 [2,] 3660 3631 5860 5982 6636 7697 [3,] 2807 2196 5440 5011 5509 6748 [4,] 1415 1840 2409 3618 3519 3795 [5,] 1065 1176 2457 3201 2857 3453 [6,] 3283 2311 5053 5491 5996 6885
示例
M4<-matrix(sample(1:50,100,replace=TRUE),ncol=10) M4
输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 14 5 42 20 33 41 11 46 2 21 [2,] 15 1 27 26 10 48 31 46 10 40 [3,] 1 43 49 4 45 31 49 18 34 12 [4,] 23 35 50 2 28 10 27 15 21 19 [5,] 6 28 40 19 25 46 41 37 34 35 [6,] 38 40 9 4 43 44 18 47 46 4 [7,] 21 35 1 14 43 31 42 3 5 44 [8,] 13 46 6 29 29 28 20 30 23 4 [9,] 48 14 21 44 11 31 14 6 19 11 [10,] 18 47 21 30 11 25 12 20 9 7
示例
PowerM4<-M4%^%2 PowerM4
输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 3832 8661 6240 3615 7610 7854 6660 6923 6274 3534 [2,] 5183 9568 5469 4276 7979 7891 6337 6571 5815 3653 [3,] 5359 8378 7006 5385 8373 10475 9294 7219 6551 6790 [4,] 3603 6376 6628 4453 6581 8304 7126 6141 4600 5153 [5,] 6483 10241 7044 5898 9008 10430 8618 7793 7034 5717 [6,] 6432 7345 6783 6596 7825 11111 7150 9289 6433 5777 [7,] 4531 6863 5664 4518 6320 8695 6486 6978 4378 5971 [8,] 4769 5906 5711 4483 5927 8056 6149 6824 4836 5144 [9,] 4641 5794 7022 3365 6615 7067 5294 6310 4342 4091 [10,] 3754 5193 5687 3389 5471 6937 5509 6230 4128 4321
示例
M5<-matrix(rnorm(36,2,5),ncol=6) M5
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 6.3775900 1.3315642 16.556120 6.7855167 1.448674 -0.4512223 [2,] -1.4833206 1.4107878 10.934331 5.3026678 4.185240 1.3263026 [3,] 5.2464593 0.7680077 5.282309 5.8267211 2.290441 -3.2177754 [4,] 1.1926252 6.2123900 -3.914076 -0.8400293 6.985479 1.0936410 [5,] -3.0834922 5.2982841 -2.171566 12.1644126 9.244374 2.4343071 [6,] -0.8845731 1.6943283 5.397350 -5.6692890 5.394338 -9.0250482
示例
PowerM5<-M5%^%2 PowerM5
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 129.58427 72.15120 175.462119 161.28433 111.09090 -39.36579 [2,] 38.05959 65.77686 25.941451 100.06454 111.68620 -28.62635 [3,] 62.76688 55.00765 78.013912 111.66072 67.43189 22.64256 [4,] -45.65285 40.99183 61.019494 97.70832 83.37095 26.51194 [5,] -55.06803 130.37502 -59.136745 82.95197 196.29795 29.24349 [6,] 4.75088 -16.57282 -5.843755 155.97795 -20.24755 73.66168
示例6
M6<-matrix(rpois(36,5),ncol=6) M6
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 4 7 11 8 3 4 [2,] 4 5 7 6 5 6 [3,] 6 1 2 7 4 1 [4,] 5 8 4 6 7 3 [5,] 7 2 3 5 7 2 [6,] 9 7 3 7 1 2
示例
PowerM6<-M6%^%3 PowerM6
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 5901 5506 5622 6925 5158 3312 [2,] 5271 4988 5166 6261 4618 3022 [3,] 3663 3259 3376 4264 3201 2043 [4,] 5599 5021 5139 6482 4769 3047 [5,] 4395 3950 4091 5141 3819 2422 [6,] 5282 4638 4659 6103 4459 2865
广告