Let's say I have a list of matrices with equal dimensions. For example:
mat = matrix(c(1,2,3,11,12,13,21,22,23), nrow = 3, ncol = 3)
mat.list = rep(list(mat), 3)
mat.list[[2]] = mat.list[[2]]*2.5
mat.list[[3]] = mat.list[[3]]*3.5
What I want is to populate a super matrix with dimensions length(mat.list)*nrow(mat) by length(mat.list)*ncol(mat) - possibly initialized as follows:
super.mat = matrix(NA, nrow = length(mat.list)*nrow(mat), ncol = length(mat.list)*ncol(mat))
according to this rule: super.mat[N*(i-1)+n,N*(j-1)+n] = mat.list[[n]][i,j]
where:
N = length(mat.list)
i and j indicate row and column indices in matrix n in mat.list
I think something in the lines of:
populateMat = function(N, n, i, j, mat, super.mat){
super.mat[N*(i-1)+n,N*(j-1)+n] = mat[i,j]
}
combined with some apply function over mat.list
that executes:
outer(1:nrow(mat), 1:ncol(mat), Vectorize(function(i,j) populateMat(N,1,i,j,mat,super.mat)))
where here mat
is just the a single element from mat.list
, should work but obviously I need a bit of help to actually make it work.