如何在 R 中按元素乘以两个矩阵?
在 R 中按元素乘以两个矩阵,我们需要将其中一个矩阵用作向量。例如,如果我们有两个名为 M1 和 M2 的矩阵,则可以通过使用 M1*as.vector(M2) 来按元素乘以这些矩阵。在进行此类乘法时,我们需要记住的主要事项是两个矩阵的行数相等。
示例
M1<-matrix(1:25,ncol=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
示例
M2<-matrix(1:25,ncol=5) M2
输出
[,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
示例
M1*as.vector(M2)
输出
[,1] [,2] [,3] [,4] [,5] [1,] 1 36 121 256 441 [2,] 4 49 144 289 484 [3,] 9 64 169 324 529 [4,] 16 81 196 361 576 [5,] 25 100 225 400 625
示例
M3<-matrix(rnorm(36),nrow=6) M3
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.66269194 -1.7457905 0.4271430 1.81482482 -1.52828812 0.1507425 [2,] 0.09297130 -0.5364919 0.8380242 -0.02126481 -0.03926208 0.2150862 [3,] 0.84724054 1.2285630 -0.8867815 1.27482991 0.76055452 0.3949002 [4,] -1.65257106 -0.7111136 2.0442647 -0.69539458 -1.89791811 1.8776415 [5,] -1.65376676 -1.7332403 0.5251878 1.20379635 0.21809336 -1.4143876 [6,] 0.09506334 -0.1884270 1.8049270 0.64850025 -0.36684996 0.7390629
示例
M4<-matrix(rnorm(36),nrow=6) M4
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.7435446 -1.6258520 -0.3318020 -1.3084350 0.2875227 -0.99886827 [2,] -1.6527269 -0.5240276 -0.6695268 0.2736843 0.9960383 -1.03338278 [3,] -2.0807263 -1.1482815 -0.6361266 -2.6593496 0.5059342 -0.07586589 [4,] 0.6829553 0.6814575 -0.1174571 -0.4193767 -0.6077792 -0.04488023 [5,] -0.4490323 -1.0317825 -1.7346983 0.8237790 0.6099129 0.42651656 [6,] 0.8599890 -0.1197610 1.1092640 -0.6074690 0.6888512 -0.58980418
示例
M3*as.vector(M4)
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.49274103 2.83839710 -0.1417269 -2.374580244 -0.43941754 -0.15057195 [2,] -0.15365618 0.28113657 -0.5610796 -0.005819844 -0.03910654 -0.22226640 [3,] -1.76287571 -1.41073618 0.5641054 -3.390218430 0.38479058 -0.02995946 [4,] -1.12863221 -0.48459370 -0.2401134 0.291632294 1.15351521 -0.08426899 [5,] 0.74259470 1.78832703 -0.9110424 0.991662178 0.13301795 -0.60325973 [6,] 0.08175343 0.02256622 2.0021405 -0.393943794 -0.25270505 -0.43590241
示例
M5<-matrix(rpois(64,5),nrow=8) M5
输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 3 3 2 3 4 7 10 6 [2,] 6 6 7 4 2 7 7 0 [3,] 6 3 9 8 12 8 7 5 [4,] 4 6 4 7 3 2 5 4 [5,] 4 5 6 6 4 5 6 4 [6,] 7 7 8 4 2 5 3 6 [7,] 2 0 4 6 3 5 5 7 [8,] 7 4 3 3 10 8 8 4
示例
M6<-matrix(rpois(64,5),nrow=8) M6
输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 5 9 5 6 9 5 7 9 [2,] 6 5 4 13 7 4 6 4 [3,] 8 4 5 5 8 7 1 6 [4,] 1 4 1 3 5 3 5 2 [5,] 5 7 5 2 5 4 10 3 [6,] 2 6 6 3 4 8 2 6 [7,] 10 6 7 7 6 6 8 4 [8,] 9 1 4 11 5 7 6 2
示例
M5*as.vector(M6)
输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 15 27 10 18 36 35 70 54 [2,] 36 30 28 52 14 28 42 0 [3,] 48 12 45 40 96 56 7 30 [4,] 4 24 4 21 15 6 25 8 [5,] 20 35 30 12 20 20 60 12 [6,] 14 42 48 12 8 40 6 36 [7,] 20 0 28 42 18 30 40 28 [8,] 63 4 12 33 50 56 48 8
示例
M8−-matrix(sample(0:9,36,replace=TRUE),nrow=6) M8
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 3 3 2 9 [2,] 2 2 1 4 3 6 [3,] 5 3 4 0 0 8 [4,] 4 0 9 1 5 8 [5,] 8 9 4 7 0 5 [6,] 2 3 1 5 8 1
示例
M7*as.vector(M8)
输出
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 4 7 24 9 4 63 [2,] 6 14 3 24 6 24 [3,] 0 24 36 0 0 16 [4,] 12 0 45 3 45 0 [5,] 16 18 4 14 0 0 [6,] 10 21 9 10 32 3
示例
M9<-matrix(sample(501:999,16),ncol=4) M9
输出
[,1] [,2] [,3] [,4] [1,] 591 926 909 660 [2,] 872 976 625 868 [3,] 801 929 690 697 [4,] 992 599 662 879
示例
M10<-matrix(sample(501:999,16),ncol=4) M10
输出
[,1] [,2] [,3] [,4] [1,] 731 772 750 854 [2,] 798 858 641 635 [3,] 613 579 560 790 [4,] 589 760 504 876
示例
M9*as.vector(M10)
输出
[,1] [,2] [,3] [,4] [1,] 432021 714872 681750 563640 [2,] 695856 837408 400625 551180 [3,] 491013 537891 386400 550630 [4,] 584288 455240 333648 770004
示例
M11<-matrix(sample(501:999,16),ncol=4) M11
输出
[,1] [,2] [,3] [,4] [1,] 690 550 737 749 [2,] 771 519 652 745 [3,] 522 841 733 825 [4,] 842 752 800 934
示例
M12<-matrix(sample(501:999,8),ncol=2) M12
输出
[,1] [,2] [1,] 969 784 [2,] 648 833 [3,] 914 808 [4,] 947 585
示例
M11*as.vector(M12)
输出
[,1] [,2] [,3] [,4] [1,] 668610 431200 714153 587216 [2,] 499608 432327 422496 620585 [3,] 477108 679528 669962 666600 [4,] 797374 439920 757600 546390
广告