行列Mと、可能な値が異なる小さな行列が与えられた場合、これらの小さな行列の組み合わせを行列Mに重ね合わせた結果として生じる可能性のあるすべての行列をリストします。小さな行列は、同じ行/列を持つMの位置に挿入されます。名前。
たとえば、次のように言います。
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
最初の小さな行列セットには3つの可能性があり、2番目には2つの可能性があるため、forループを使用せずに、6つの可能な行列すべてをリストとして出力しようとします。
[[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
一般に、それぞれが独自の数の行列を持つ「小さな行列のリスト」をn個指定することができます。このコンテキストでapplytype関数をどのように使用しますか?