Split a matrix into a list of blocks (either by rows or by columns)

matrix_to_blocks(Matrix, blocks, byrow = TRUE)

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. If blocks 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

Value

A list of matrices

Author

Gaston Sanchez

See also

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.333516  1.0229750 -0.1389818 -0.3318437 -0.3664693  0.3312638
#> [2,]  1.740138 -1.6911442  2.7308061 -0.9159330 -0.7477748  0.4303582
#> [3,]  3.676596 -0.5893431  0.6826662  0.5437173 -1.7425781 -0.6301231
#>            [,7]
#> [1,] -1.2201014
#> [2,]  0.2358788
#> [3,] -1.1874375
#> 
#> [[2]]
#>           [,1]          [,2]       [,3]      [,4]      [,5]       [,6]
#> [1,] 0.7250420 -0.0005062043 0.27905441 -1.065672 -1.109569  2.0205857
#> [2,] 0.1450109 -0.7360230462 0.01487457 -1.514387  1.113836 -0.9881449
#>             [,7]
#> [1,] -0.09779199
#> [2,] -1.58698737
#> 
#> [[3]]
#>              [,1]         [,2]       [,3]       [,4]       [,5]       [,6]
#> [1,] -0.293653448  0.994442832  0.4571903 -0.3947118  1.8990917 -0.2887112
#> [2,]  0.600989971 -0.251166858 -0.9046805 -0.4737186 -0.6608972  1.9126630
#> [3,]  0.664280586 -0.710994405 -0.3068376 -1.5053322 -1.8589667 -1.7442175
#> [4,]  0.004414104  0.182786220 -1.0879656 -1.7395430 -0.6630963 -0.7551556
#> [5,]  0.082652589  0.008086858  1.4535832 -1.1587191  1.3037711  0.4954030
#>            [,7]
#> [1,] -0.6539435
#> [2,] -0.3245454
#> [3,] -0.6911203
#> [4,] -0.4474582
#> [5,] -0.3598401
#> 

# 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.333515545  1.0229749642 -0.13898176
#>  [2,]  1.740137930 -1.6911442196  2.73080611
#>  [3,]  3.676595856 -0.5893431235  0.68266615
#>  [4,]  0.725042032 -0.0005062043  0.27905441
#>  [5,]  0.145010856 -0.7360230462  0.01487457
#>  [6,] -0.293653448  0.9944428321  0.45719028
#>  [7,]  0.600989971 -0.2511668577 -0.90468052
#>  [8,]  0.664280586 -0.7109944054 -0.30683755
#>  [9,]  0.004414104  0.1827862198 -1.08796561
#> [10,]  0.082652589  0.0080868584  1.45358320
#> 
#> [[2]]
#>             [,1]       [,2]       [,3]        [,4]
#>  [1,] -0.3318437 -0.3664693  0.3312638 -1.22010144
#>  [2,] -0.9159330 -0.7477748  0.4303582  0.23587883
#>  [3,]  0.5437173 -1.7425781 -0.6301231 -1.18743753
#>  [4,] -1.0656719 -1.1095688  2.0205857 -0.09779199
#>  [5,] -1.5143872  1.1138355 -0.9881449 -1.58698737
#>  [6,] -0.3947118  1.8990917 -0.2887112 -0.65394346
#>  [7,] -0.4737186 -0.6608972  1.9126630 -0.32454538
#>  [8,] -1.5053322 -1.8589667 -1.7442175 -0.69112030
#>  [9,] -1.7395430 -0.6630963 -0.7551556 -0.44745820
#> [10,] -1.1587191  1.3037711  0.4954030 -0.35984012
#>