I'm looking for a (build-in) function, which efficiently returns the list of building blocks of a block-diagonal matrix in the following way (rather than iterating over the slots to get the list manually):
#construct bdiag-matrix
library("Matrix")
listElems <- list(matrix(1:4,ncol=2,nrow=2),matrix(5:8,ncol=2,nrow=2))
mat <- bdiag(listElems)
#get back the list
res <- theFunctionImLookingFor(mat)
The result res
yields the building blocks:
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[,1] [,2]
[1,] 5 7
[2,] 6 8
Edit: Regarding my use case, the list elements in listElems
are square and symmetric matrices. If the block is a diagonal matrix, theFunctionImLookingFor
should return a list element for each diagonal element.
However, the function should be able to deal with building block matrices like
[,1] [,2] [,3]
[1,] 1 1 0
[2,] 1 1 1
[3,] 0 1 1
or
[,1] [,2] [,3]
[1,] 1 0 1
[2,] 0 1 1
[3,] 1 1 1
i.e. deal with zeros in blocks, which are not diagonal matrices.
mat@i
for this. However, although my brain's pattern recognition is able to identify the blocks from something like[1] 0 1 0 1 2 3 4 2 3 4 2 3 4 5
(this is for a little bit more complex example) easily, I can't find a good algorithm right now. – RolandtheFunctionImLookingFor <- list
? It always return one block... We need more details for this to be a well-posed problem. – flodelbdiag
. – Rolandmat
can be broken into many blocks, in what way is the one the OP chose special apart from the fact they are those that were used to buildmat
(that info is lost, isn't it?). – flodel