How to split a matrix with multiple rows into many matrices

I have a matrix(360×10), and I want to split it into 360 matrices. The first matrix is the first row of the original matrix, and the second matrix is the first two rows of the original matrix……In this way, each subsequent matrix has one row more than the previous matrix.

Here is one way that will keep all the matrices together in a list


m <- matrix(1:12, 4)
lapply(seq_len(nrow(m)), function(i) matrix(m[1:i, ], ncol = ncol(m)))
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> [2,]    2    6   10
#> 
#> [[3]]
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> [2,]    2    6   10
#> [3,]    3    7   11
#> 
#> [[4]]
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> [2,]    2    6   10
#> [3,]    3    7   11
#> [4,]    4    8   12

Created on 2020-10-16 by the reprex package (v0.3.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.