Given a matrix M and smaller matrices with different possible values, I seek to list all possible matrices resulting from superimposing combinations of these small matrices into the matrix M. The small matrices are to be inserted in the locations of M having the same row/column names.
For instance, say have:
M <- matrix(rep(0, 49), nrow =7, ncol =7)
rownames(M) <- colnames(M) <-seq(1,7)
> M
1 2 3 4 5 6 7
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0
# Generate first set of small matrices:
sub_mat_1_1 <- matrix(rep(1, 9), nrow =3, ncol =3)
rownames(sub_mat_1_1) <- colnames(sub_mat_1_1) <- c(2,3,5)
sub_mat_1_2 <- matrix(rep(2, 9), nrow =3, ncol =3)
rownames(sub_mat_1_2) <- colnames(sub_mat_1_2) <- c(2,3,5)
sub_mat_1_3 <- matrix(rep(3, 9), nrow =3, ncol =3)
rownames(sub_mat_1_3) <- colnames(sub_mat_1_3) <- c(2,3,5)
submatrix_1 <- list(sub_mat_1_1, sub_mat_1_2, sub_mat_1_3)
# Generate second set of small matrices:
submatrix_2 <- list()
sub_mat_2_1 <- matrix(rep(1, 4), nrow =2, ncol =2)
rownames(sub_mat_2_1) <- colnames(sub_mat_2_1) <- c(1,6)
sub_mat_2_2 <- matrix(rep(2, 4), nrow =2, ncol =2)
rownames(sub_mat_2_2) <- colnames(sub_mat_2_2) <- c(1,6)
submatrix_2 <- list(sub_mat_2_1, sub_mat_2_2)
# Generate list of small matrices:
submatrices <- list()
submatrices[[1]] <- submatrix_1
submatrices[[2]] <- submatrix_2
[[1]]
[[1]][[1]]
2 3 5
2 1 1 1
3 1 1 1
5 1 1 1
[[1]][[2]]
2 3 5
2 2 2 2
3 2 2 2
5 2 2 2
[[1]][[3]]
2 3 5
2 3 3 3
3 3 3 3
5 3 3 3
[[2]]
[[2]][[1]]
1 6
1 1 1
6 1 1
[[2]][[2]]
1 6
1 2 2
6 2 2
Since there are 3 possibilities for the first small matrix set and 2 for the second, I seek to output, without using a for loop, all 6 possible matrices as a list:
[[1]]
1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 1 1 0 1 0 0
3 0 1 1 0 1 0 0
4 0 0 0 0 0 0 0
5 0 1 1 0 1 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0
[[2]]
1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 2 2 0 2 0 0
3 0 2 2 0 2 0 0
4 0 0 0 0 0 0 0
5 0 2 2 0 2 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0
[[3]]
1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 3 3 0 3 0 0
3 0 3 3 0 3 0 0
4 0 0 0 0 0 0 0
5 0 3 3 0 3 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0
[[4]]
1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 1 1 0 1 0 0
3 0 1 1 0 1 0 0
4 0 0 0 0 0 0 0
5 0 1 1 0 1 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0
[[5]]
1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 2 2 0 2 0 0
3 0 2 2 0 2 0 0
4 0 0 0 0 0 0 0
5 0 2 2 0 2 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0
[[6]]
1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 3 3 0 3 0 0
3 0 3 3 0 3 0 0
4 0 0 0 0 0 0 0
5 0 3 3 0 3 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0
In general, I may have n given “lists of small matrices”, each with its own number of matrices. How would I use an apply type function in this context?
This gives the requested output, but I have to agree with the commenters that the problem is not very well posed. How do you define the positions into which the values of the submatrices are inserted? I simply assumed that you wanted them inserted into a particular subset of rows and columns as shown in your requested output …
A function to insert two matrices into specified rows/columns of
M(defined globally, probably bad practice)Now use
expand.gridto create a data frame with all combinations of indices from each list of submatrices, and usealply(array-to-list) fromplyrto runtmpmatfon each combination:This should work with an arbitrary number of submatrices in each your two lists of submatrices, but if you really have more (>2) lists of submatrices, then you haven’t given us enough information to specify how (e.g.) the third list of submatrices should be pasted into the larger matrix …
Note that the first part of this (using matrix indexing via a two-column matrix) is a lot faster than using a
forloop to insert individual elements into the matrix, but the second part (alply) is not actually much (any?) faster than a pair of nestedforloops that iterate over all combinations — the latter might be clearer/easier to debug in this case …