List of combination between rows of diagonal block matrix

旧城冷巷雨未停 提交于 2019-12-13 15:25:22

问题


I have the following R matrix that is a combination of 2x3 and 3x3 submatrices and it can be more than 2 submatrices with different dimension (e.g. m1xp and m2xp and m3xp where each of m1,m2,m3 <= p)

A2 <- list(rbind(c(1,1,1),c(-1,1,-1)),
           rbind(c(-1,1,1),c(1,-1,2),c(2,-1,2)))
library(Matrix)
A2 <- as.matrix(Matrix::bdiag(A2))
Rhs <- matrix(c(0,5,0.5,4),nrow = 4)
beta <- c(rep(1.2,3),c(0.5,0.2,0.1))
> A2
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    0    0    0
[2,]   -1    1   -1    0    0    0
[3,]    0    0    0   -1    1    1
[4,]    0    0    0    1   -1    2
[5,]    0    0    0    2   -1    2

I would like to get all the rows indices combination between the first sub-matrix and the 2nd sub-matrix to solve an linear optimization problem. The combination has to be from both submatrices then solve for new beta and then check if the condition Aq %*% beta == Rhs is satisfy, stop. If not, then take another combination. I think below is all the rows combination between the sub-matrices:

A combination as one from the first sub-matrix and one from the second sub-matrix

Aq <- A2[c(1,3),]
Aq <- A2[c(1,4),]
Aq <- A2[c(1,5),]
Aq <- A2[c(2,3),]
Aq <- A2[c(2,4),]
Aq <- A2[c(2,5),]

Then, a combination as one from the first and 2 from the second matrix

Aq <- A2[c(1,3,4),]
Aq <- A2[c(1,3,5),]
Aq <- A2[c(1,4,5),]
Aq <- A2[c(2,3,4),]
Aq <- A2[c(2,3,5),]
Aq <- A2[c(2,4,5),]

Then, a combination as one from the first and 3 from the second matrix

Aq <- A2[c(1,3,4,5),]
Aq <- A2[c(2,3,4,5),]

Then, a combination as 2 from the first and one from the second matrix

Aq <- A2[c(1,2,3),]
Aq <- A2[c(1,2,4),]
Aq <- A2[c(1,2,5),]

Then, a combination as 2 from the first and 2 from the second matrix

Aq <- A2[c(1,2,3,4),]
Aq <- A2[c(1,2,3,5),]
Aq <- A2[c(1,2,4,5),]

Then, a combination as 2 from the first and 3 from the second matrix

Aq <- A2[c(1,2,3,4,5),]

Is there a better way to get all the combinations? Then I would like to create a loop that choice one on the above combination at a time and check if

if (Aq %*% beta == Rhs) {
  break
} else {
  TAKE ANOTHER COMBINATION Aq
}

Please note I could have more than 2 submatrices that create the block matrix. Then I have to create all row combinations between from the first, 2nd and 3rd matrix. I am hoping there is easy way to do in R. I have tried grid.expand function but it is not giving me the desired output.


回答1:


A possible base R approach:

indices1 <- 1:2
indices2 <- 3:5
apply(expand.grid(seq_along(indices1), seq_along(indices2)), 1, 
    function(x) t(apply(
                      expand.grid(combn(indices1, x[1], simplify=FALSE), 
                            combn(indices2, x[2], simplify=FALSE)), 
                  1, unlist)))

output:

[[1]]
     Var1 Var2
[1,]    1    3
[2,]    2    3
[3,]    1    4
[4,]    2    4
[5,]    1    5
[6,]    2    5

[[2]]
     Var11 Var12 Var2
[1,]     1     2    3
[2,]     1     2    4
[3,]     1     2    5

[[3]]
     Var1 Var21 Var22
[1,]    1     3     4
[2,]    2     3     4
[3,]    1     3     5
[4,]    2     3     5
[5,]    1     4     5
[6,]    2     4     5

[[4]]
     Var11 Var12 Var21 Var22
[1,]     1     2     3     4
[2,]     1     2     3     5
[3,]     1     2     4     5

[[5]]
     Var1 Var21 Var22 Var23
[1,]    1     3     4     5
[2,]    2     3     4     5

[[6]]
     Var11 Var12 Var21 Var22 Var23
[1,]     1     2     3     4     5

edit: adding a more general version:

#identifying the indices
indices <- split(seq_len(nrow(A2)), max.col(abs(A2) > 0, "first"))

#generating the combinations
apply(expand.grid(lapply(indices, seq_along)), 1L, 
    function(idx) {
        t(apply(
            expand.grid(
                lapply(seq_along(idx), 
                    function(k) {
                        combn(indices[[k]], idx[k], simplify=FALSE)
                    })),
            1L, unlist))
    })


来源:https://stackoverflow.com/questions/54298621/list-of-combination-between-rows-of-diagonal-block-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!