Initializing a matrix of matrices

Well, if B is a matrix of matrices, e.g. B = [rand(2,2) for i = 1:50, j=1:50], then you could convert it to a single 100x100 matrix M with e.g.:

M = zeros(100,100)
for i=1:50, j=1:50
    M[2i-1:2i,2j-1:2j] = B[i,j]
end

Another option is to use the BlockArrays.jl package. Then if you have an array of arrays, like B above, you can treat it as a single 100x100 matrix (and do linear algebra directly) with mortar(B), or convert it to an ordinary matrix with Matrix(mortar(B)).

2 Likes