简体   繁体   English

将大矩阵分成子集,R

[英]split big matrix in subsets,R

I would like to make subgroups for a matrix, and each subgroup contains the same amount of column. 我想为矩阵创建子组,每个子组包含相同数量的列。 For example, there is a matrix with 1000 rows and 420 columns, and I would like to split into 35 sub-matrix in order, like first one contains the first 12 cols, and the second contains the second 12 cols, and so on. 例如,有一个具有1000行和420列的矩阵,我想按顺序拆分为35个子矩阵,例如第一个包含前12个列,第二个包含第二个12列,依此类推。 I think I could use function Iris. 我想我可以使用功能虹膜。 Please help me! 请帮我!

iris[c()]

Not sure if this is what you're after, but here's a reproducible example: 不知道这是您要追求的,但是这是一个可复制的示例:

# Define matrix
M = matrix( 
     c(1:20), 
     nrow=2, 
     ncol=10) 

# Split into 5 submatrices of equal size
lapply(split(M, rep(1:5, each = 4)), matrix, ncol = 2)
$`1`
     [,1] [,2]
[1,]    1    3
[2,]    2    4

$`2`
     [,1] [,2]
[1,]    5    7
[2,]    6    8

$`3`
     [,1] [,2]
[1,]    9   11
[2,]   10   12

$`4`
     [,1] [,2]
[1,]   13   15
[2,]   14   16

$`5`
     [,1] [,2]
[1,]   17   19
[2,]   18   20

You can turn your 1000 × 420 matrix into a 1000 × 12 × 35 matrix with 您可以将1000×420矩阵转换为1000×12×35矩阵

dim(x) <- c(1000, 12, 35)

where x is the original matrix. 其中x是原始矩阵。 Then x[, , 1] gives you the first 1000 × 12 sub-matrix, x[, , 2] the second sub-matrix, and so forth. 然后x[, , 1]为您提供第一个1000×12子矩阵, x[, , 2]为第二个子矩阵,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM