Split a matrix into blocks
matrix_to_blocks.Rd
Split a matrix into a list of blocks (either by rows or by columns)
Arguments
- Matrix
a matrix to split
- blocks
either a list or a vector indicating the blocks. If
blocks
is a list of vectors, then the length of each vector defines the size of the blocks. Ifblocks
is a vector, then each element represents the size of the blocks.- byrow
logical. If
TRUE
(the default) the matrix is split by rows, otherwise the matrix is split by columns
Examples
# matrix with 10 rows and 7 columns
M = matrix(rnorm(70), 10, 7)
# row blocks
row_sets = list(1:3, 4:5, 6:10)
# split matrix by rows
matrix_to_blocks(M, row_sets)
#> [[1]]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 1.6235489 -0.2514834 0.01917759 -0.2458964 0.2366963 0.5562243
#> [2,] 0.1120381 0.4447971 0.02956075 -1.1775633 1.3182934 -0.5482573
#> [3,] -0.1339970 2.7554176 0.54982754 -0.9758506 0.5239098 1.1105349
#> [,7]
#> [1,] -0.03810289
#> [2,] 0.48614892
#> [3,] 1.67288261
#>
#> [[2]]
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] -1.9100875 0.04653138 -2.274115 1.0650573 0.6067480 -2.6123343 -0.3543612
#> [2,] -0.2792372 0.57770907 2.682557 0.1316706 -0.1099357 -0.1556938 0.9463479
#>
#> [[3]]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] -0.31344598 0.1181949 -0.3612213 0.4886288 0.17218172 0.4338898
#> [2,] 1.06730788 -1.9117205 0.2133557 -1.6994506 -0.09032729 -0.3819511
#> [3,] 0.07003485 0.8620865 1.0743459 -1.4707363 1.92434334 0.4241876
#> [4,] -0.63912332 -0.2432367 -0.6650882 0.2841503 1.29839276 1.0631020
#> [5,] -0.04996490 -0.2060872 1.1139524 1.3373204 0.74879127 1.0487126
#> [,7]
#> [1,] 1.3168264
#> [2,] -0.2966400
#> [3,] -0.3872136
#> [4,] -0.7854327
#> [5,] -1.0567369
#>
# column blocks
col_sets = c(3, 4)
# split matrix by rows
matrix_to_blocks(M, col_sets, byrow=FALSE)
#> [[1]]
#> [,1] [,2] [,3]
#> [1,] 1.62354888 -0.25148344 0.01917759
#> [2,] 0.11203808 0.44479712 0.02956075
#> [3,] -0.13399701 2.75541758 0.54982754
#> [4,] -1.91008747 0.04653138 -2.27411486
#> [5,] -0.27923724 0.57770907 2.68255718
#> [6,] -0.31344598 0.11819487 -0.36122126
#> [7,] 1.06730788 -1.91172049 0.21335575
#> [8,] 0.07003485 0.86208648 1.07434588
#> [9,] -0.63912332 -0.24323674 -0.66508825
#> [10,] -0.04996490 -0.20608719 1.11395242
#>
#> [[2]]
#> [,1] [,2] [,3] [,4]
#> [1,] -0.2458964 0.23669628 0.5562243 -0.03810289
#> [2,] -1.1775633 1.31829338 -0.5482573 0.48614892
#> [3,] -0.9758506 0.52390979 1.1105349 1.67288261
#> [4,] 1.0650573 0.60674805 -2.6123343 -0.35436116
#> [5,] 0.1316706 -0.10993567 -0.1556938 0.94634789
#> [6,] 0.4886288 0.17218172 0.4338898 1.31682636
#> [7,] -1.6994506 -0.09032729 -0.3819511 -0.29664002
#> [8,] -1.4707363 1.92434334 0.4241876 -0.38721358
#> [9,] 0.2841503 1.29839276 1.0631020 -0.78543266
#> [10,] 1.3373204 0.74879127 1.0487126 -1.05673687
#>