如何在R中用下三角矩阵替换上三角矩阵,反之亦然?
通过对整个矩阵进行转置并从中提取上三角矩阵,然后将其存储在原始矩阵中,可以将上三角矩阵替换为下三角矩阵。例如,如果我们有一个矩阵 M,那么 M 的上三角矩阵可以使用下面的代码替换为下三角矩阵 -
M1[upper.tri(M1)]<-t(M1)[upper.tri(M1)]
也可以用类似的方式对下三角矩阵进行。
示例
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
示例
M1[upper.tri(M1)]<-t(M1)[upper.tri(M1)]输出结果M1
[,1] [,2] [,3] [,4] [,5][1,] 1 2 3 4 5
[2,] 2 7 8 9 10
[3,] 3 8 13 14 15
[4,] 4 9 14 19 20
[5,] 5 10 15 20 25
示例
M2<-matrix(rpois(100,10),nrow=10)输出结果M2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 3 12 16 6 4 11 12 12 9 14
[2,] 12 9 9 11 10 14 13 10 10 13
[3,] 17 14 8 12 12 15 9 8 15 4
[4,] 17 6 9 17 11 8 11 15 14 12
[5,] 11 14 7 7 12 9 9 8 14 13
[6,] 13 7 11 8 10 3 13 11 6 11
[7,] 13 11 7 11 15 9 12 10 11 7
[8,] 13 10 3 8 7 11 13 9 7 11
[9,] 7 18 15 11 12 14 10 5 7 15
[10,] 13 7 15 11 11 9 12 6 11 10
示例
M2[upper.tri(M2)]<-t(M2)[upper.tri(M2)]输出结果M2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 3 12 17 17 11 13 13 13 7 13
[2,] 12 9 14 6 14 7 11 10 18 7
[3,] 17 14 8 9 7 11 7 3 15 15
[4,] 17 6 9 17 7 8 11 8 11 11
[5,] 11 14 7 7 12 10 15 7 12 11
[6,] 13 7 11 8 10 3 9 11 14 9
[7,] 13 11 7 11 15 9 12 13 10 12
[8,] 13 10 3 8 7 11 13 9 5 6
[9,] 7 18 15 11 12 14 10 5 7 11
[10,] 13 7 15 11 11 9 12 6 11 10
示例
M3<-matrix(rpois(64,5),nrow=8)输出结果M3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8][1,] 8 5 2 5 6 6 8 3
[2,] 7 6 13 5 6 9 5 8
[3,] 6 3 12 2 2 8 3 3
[4,] 3 6 2 7 3 1 5 6
[5,] 1 8 7 6 9 3 3 10
[6,] 4 4 7 10 4 4 8 4
[7,] 9 3 9 4 5 8 3 11
[8,] 4 4 9 5 5 4 8 6
示例
M3[lower.tri(M3)]<-t(M3)[lower.tri(M3)]输出结果M3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8][1,] 8 5 2 5 6 6 8 3
[2,] 5 6 13 5 6 9 5 8
[3,] 2 13 12 2 2 8 3 3
[4,] 5 5 2 7 3 1 5 6
[5,] 6 6 2 3 9 3 3 10
[6,] 6 9 8 1 3 4 8 4
[7,] 8 5 3 5 3 8 3 11
[8,] 3 8 3 6 10 4 11 6
示例
M4<-matrix(rnorm(36),nrow=6)输出结果M4
[,1] [,2] [,3] [,4] [,5] [,6][1,] 0.05023214 -0.3817478 0.7079296 0.8578374 -1.5239053 0.1864117
[2,] -0.73729460 1.5129931 0.2135871 0.9721983 -0.2416306 -0.5309397
[3,] -0.24488229 -0.6229905 0.8579926 1.3248632 -1.9305997 0.5397310
[4,] -1.24118039 -1.1080773 -0.9903481 0.1033527 0.4685492 0.1691699
[5,] 1.60606286 -0.9149534 -1.3265918 2.0232826 0.5383570 0.9284483
[6,] 1.29482725 -0.7275765 1.1407260 -0.8860395 -0.3185204 -1.0165007
示例
M4[lower.tri(M4)]<-t(M4)[lower.tri(M4)]输出结果M4
[,1] [,2] [,3] [,4] [,5] [,6][1,] 0.05023214 -0.3817478 0.7079296 0.8578374 -1.5239053 0.1864117
[2,] -0.38174778 1.5129931 0.2135871 0.9721983 -0.2416306 -0.5309397
[3,] 0.70792961 0.2135871 0.8579926 1.3248632 -1.9305997 0.5397310
[4,] 0.85783742 0.9721983 1.3248632 0.1033527 0.4685492 0.1691699
[5,] -1.52390534 -0.2416306 -1.9305997 0.4685492 0.5383570 0.9284483
[6,] 0.18641175 -0.5309397 0.5397310 0.1691699 0.9284483 -1.0165007
示例
M5<-matrix(rexp(9),nrow=3)输出结果M5
[,1] [,2] [,3][1,] 0.8228933 0.05741123 0.3246455
[2,] 0.1750773 0.08412468 2.1097830
[3,] 0.3997029 1.18019289 0.1099468
示例
M5[upper.tri(M5)]<-t(M5)[upper.tri(M5)]输出结果M5
[,1] [,2] [,3][1,] 0.8228933 0.17507725 0.3997029
[2,] 0.1750773 0.08412468 1.1801929
[3,] 0.3997029 1.18019289 0.1099468
以上是 如何在R中用下三角矩阵替换上三角矩阵,反之亦然? 的全部内容, 来源链接: utcz.com/z/360617.html