对角块矩阵行之间的组合列表 [英] List of combination between rows of diagonal block matrix

查看:152
本文介绍了对角块矩阵行之间的组合列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下R矩阵,它是2x3和3x3子矩阵的组合,并且可以是两个以上具有不同维度的子矩阵(例如m1xp和m2xp和m3xp,其中m1,m2,m3分别== p)

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

我想获得第一个子矩阵和第二个子矩阵之间的所有行索引组合,以解决线性优化问题.组合必须来自两个子矩阵,然后求解新的beta,然后检查是否满足条件Aq %*% beta == Rhs,然后停止.如果不是,则采取另一种组合.我认为下面是子矩阵之间的所有行组合:

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),]

然后,将第一个矩阵中的一个与第二个矩阵中的2组合为一个

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),]

然后,将第一个矩阵中的一个与第二个矩阵中的3组合为一个

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),]

然后,第一个矩阵的组合为2,第二个矩阵的组合为

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),]

然后,第一个矩阵的组合为2,第二个矩阵的组合为

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),]

然后,第一个矩阵的组合为2,第二个矩阵的组合为

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
}

请注意,我可能有两个以上的子矩阵来创建块矩阵.然后,我必须创建第一个,第二个和第三个矩阵之间的所有行组合.我希望在R中有一种简单的方法.我尝试了grid.expand函数,但没有给我想要的输出.

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.

推荐答案

一种可能的基本R方法:

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)))

输出:

[[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))
    })

这篇关于对角块矩阵行之间的组合列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆