Suppose I have a square matrix (45 x 45) that I would like to split into 5 sub matrices that are 9 x 9 based off of the diagonal blocks in the matrix. I'm looking for a general way to accomplish this without specifying col and rows.
Example
mat <- matrix(rnorm(45), 45, 45)
mat1 <- mat[1:9, 1:9]
mat2 <- mat[10:18, 10:18]
mat3 <- mat[19:27, 19:27]
mat4 <- mat[28:36, 28:36]
mat5 <- mat[37:45, 37:45]
Take the kronecker product of the two matrices shown giving a block diagonal matrix m whose first block is all 1's, second block is all 2's, etc. Then split by that, remove the component corresponding to the off-diagonals and re-form each component of the split to a matrix. The result s is a list of the blocks.
# test inputs
set.seed(123)
n <- 45
k <- 9
mat <- matrix(round(rnorm(n*n), 2), n, n)
m <- diag(1:(n/k)) %x% matrix(1, k, k)
s <- lapply(split(mat, m)[-1], matrix, k)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With