如何在R中创建矩阵列表?
要创建矩阵列表,我们只需要在列表函数中找到矩阵对象即可。例如,如果我们有五个尺寸相同或不同的矩阵对象定义为Matrix1,Matrix2,Matrix3,Matrix4和Matrix5,则可以将这些矩阵的列表创建为-
List_of_Matrix<-list(Matrix1,Matrix2,Matrix3,Matrix4,Matrix5)
示例
请看以下矩阵-
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(rnorm(36,5,1),ncol=6)M2
输出结果
[,1] [,2] [,3] [,4] [,5] [,6][1,] 5.832047 4.945123 5.358729 3.574902 4.350990 4.087932
[2,] 4.772671 5.250141 4.988955 5.365941 4.880831 3.562414
[3,] 5.266137 5.618243 4.059351 5.248413 5.664136 4.202910
[4,] 4.623297 4.827376 4.884175 5.065288 6.100969 6.254083
[5,] 7.441365 2.776100 4.185031 5.019156 5.143771 5.772142
[6,] 4.204661 3.736386 5.242263 5.257338 4.882246 4.780484
示例
M3<-matrix(rpois(100,5),nrow=10)M3
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 4 6 6 7 3 9 2 5 3 5
[2,] 6 5 9 4 3 9 4 4 5 4
[3,] 4 8 5 7 6 4 7 5 9 9
[4,] 6 4 6 4 9 3 4 4 6 5
[5,] 7 3 4 3 2 3 3 5 9 4
[6,] 7 8 2 5 7 4 4 8 5 4
[7,] 4 5 8 4 9 5 6 3 5 7
[8,] 4 8 4 3 7 8 4 4 5 6
[9,] 8 3 5 5 4 5 6 3 2 3
[10,] 6 6 2 5 6 3 6 4 3 2
示例
M4<-matrix(runif(25,2,5),nrow=5)M4
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 4.264117 3.145149 2.543695 4.640957 2.464495
[2,] 3.861230 2.507933 3.431941 3.119190 2.396685
[3,] 2.508730 2.895958 4.312211 2.143877 2.663918
[4,] 2.186642 2.576629 2.083361 2.415885 2.679142
[5,] 2.327088 2.771510 3.581932 2.964476 2.394250
示例
M5<-matrix(sample(0:5,64,replace=TRUE),nrow=8)M5
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8][1,] 3 0 2 3 3 0 4 2
[2,] 3 0 4 3 5 0 3 2
[3,] 0 5 0 5 3 3 5 1
[4,] 1 5 0 3 4 3 0 0
[5,] 3 5 0 4 3 4 1 3
[6,] 0 3 5 1 1 4 0 1
[7,] 0 2 4 3 1 5 4 4
[8,] 2 4 3 0 1 0 1 4
示例
M6<-matrix(sample(c(5,15,20,25,30),49,replace=TRUE),nrow=7)M6
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7][1,] 20 30 15 20 20 20 15
[2,] 15 25 30 5 15 30 25
[3,] 25 30 5 30 25 15 5
[4,] 20 20 20 25 5 30 5
[5,] 20 20 15 15 5 25 5
[6,] 5 5 25 30 5 15 30
[7,] 25 5 5 30 20 15 5
创建矩阵列表-
示例
List<-list(M1,M2,M3,M4,M5,M6)List
输出结果
[[1]][,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
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5.832047 4.945123 5.358729 3.574902 4.350990 4.087932
[2,] 4.772671 5.250141 4.988955 5.365941 4.880831 3.562414
[3,] 5.266137 5.618243 4.059351 5.248413 5.664136 4.202910
[4,] 4.623297 4.827376 4.884175 5.065288 6.100969 6.254083
[5,] 7.441365 2.776100 4.185031 5.019156 5.143771 5.772142
[6,] 4.204661 3.736386 5.242263 5.257338 4.882246 4.780484
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 4 6 6 7 3 9 2 5 3 5
[2,] 6 5 9 4 3 9 4 4 5 4
[3,] 4 8 5 7 6 4 7 5 9 9
[4,] 6 4 6 4 9 3 4 4 6 5
[5,] 7 3 4 3 2 3 3 5 9 4
[6,] 7 8 2 5 7 4 4 8 5 4
[7,] 4 5 8 4 9 5 6 3 5 7
[8,] 4 8 4 3 7 8 4 4 5 6
[9,] 8 3 5 5 4 5 6 3 2 3
[10,] 6 6 2 5 6 3 6 4 3 2
[[4]]
[,1] [,2] [,3] [,4] [,5]
[1,] 4.264117 3.145149 2.543695 4.640957 2.464495
[2,] 3.861230 2.507933 3.431941 3.119190 2.396685
[3,] 2.508730 2.895958 4.312211 2.143877 2.663918
[4,] 2.186642 2.576629 2.083361 2.415885 2.679142
[5,] 2.327088 2.771510 3.581932 2.964476 2.394250
[[5]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3 0 2 3 3 0 4 2
[2,] 3 0 4 3 5 0 3 2
[3,] 0 5 0 5 3 3 5 1
[4,] 1 5 0 3 4 3 0 0
[5,] 3 5 0 4 3 4 1 3
[6,] 0 3 5 1 1 4 0 1
[7,] 0 2 4 3 1 5 4 4
[8,] 2 4 3 0 1 0 1 4
[[6]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 20 30 15 20 20 20 15
[2,] 15 25 30 5 15 30 25
[3,] 25 30 5 30 25 15 5
[4,] 20 20 20 25 5 30 5
[5,] 20 20 15 15 5 25 5
[6,] 5 5 25 30 5 15 30
[7,] 25 5 5 30 20 15 5
以上是 如何在R中创建矩阵列表? 的全部内容, 来源链接: utcz.com/z/347291.html