【问题标题】:R: Matrix Combination with specific number of valuesR:具有特定数量值的矩阵组合
【发布时间】:2019-03-08 05:08:36
【问题描述】:

我想制作我的矩阵的所有组合。
前任。一个二进制 5 X 5 矩阵,其中我只有两个 1 行(见下文)

Com 1:

1 1 0 0 0   
1 1 0 0 0    
1 1 0 0 0  
1 1 0 0 0   
1 1 0 0 0  

Com 2:

1 0 1 0 0  
1 1 0 0 0  
1 1 0 0 0  
1 1 0 0 0   
1 1 0 0 0  

。 . .

Com ?:

0 0 0 1 1  
0 0 0 1 1  
0 0 0 1 1  
0 0 0 1 1  
0 0 0 1 1  

我尝试在R 中使用Combination 包,但找不到解决方案。

【问题讨论】:

  • 你能告诉我们你的尝试吗?
  • 每行有 5 个选择 2 = 10 种可能性。这意味着您的输出将是 100,000 个大小为 5 x 5 的矩阵的列表。这是您想要的吗?
  • @Rohit 很遗憾是的.. 完全正确。我试图一次制作所有可能性矩阵,但由于我的记忆力而失败。所以,现在我找到了逐渐制作矩阵的解决方案。
  • @BenAvery 你好,我制作了可能性向量 (1, 1, 0, 0, 0), (1, 0, 1, 0, 0).... 并尝试与这些向量组合, , 但是失败了..

标签: r combinations permutation


【解决方案1】:

使用RcppAlgos(我是作者)我们可以通过 2 次调用来完成此操作。它也相当快:

library(tictoc)
library(RcppAlgos)

tic("RcppAlgos solution")

## First we generate the permutations of the multiset c(1, 1, 0, 0, 0)
binPerms <- permuteGeneral(1:0, 5, freqs = c(2, 3))

## Now we generate the permutations with repetition choose 5
## and select the rows from binPerms above
allMatrices <- permuteGeneral(1:nrow(binPerms), 5, 
                              repetition = TRUE, 
                              FUN = function(x) {
                                  binPerms[x, ]
                              })
toc()
RcppAlgos solution: 0.108 sec elapsed

这是输出:

allMatrices[1:3]
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    0    0
[2,]    1    1    0    0    0
[3,]    1    1    0    0    0
[4,]    1    1    0    0    0
[5,]    1    1    0    0    0

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    0    0
[2,]    1    1    0    0    0
[3,]    1    1    0    0    0
[4,]    1    1    0    0    0
[5,]    1    0    1    0    0

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    0    0
[2,]    1    1    0    0    0
[3,]    1    1    0    0    0
[4,]    1    1    0    0    0
[5,]    1    0    0    1    0


len <- length(allMatrices)
len
[1] 100000

allMatrices[(len - 2):len]
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    0    0    1    1
[4,]    0    0    0    1    1
[5,]    0    0    1    1    0

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    0    0    1    1
[4,]    0    0    0    1    1
[5,]    0    0    1    0    1

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    0    0    1    1
[4,]    0    0    0    1    1
[5,]    0    0    0    1    1    

【讨论】:

    【解决方案2】:

    我在下面编写的代码对我有用。 100,000 个 5x5 矩阵的列表。每行都有两个位置设置为 1。

    n <- 5 # No of columns
    k <- 2 # No. of ones
    m <- 5  # No of rows in matrix
    
    nck <- combn(1:n,k,simplify = F)
    possible_rows <-lapply(nck,function(x){
      arr <- numeric(n)
      arr[x] <- 1
      matrix(arr,nrow=1)
    })
    
    mat_list <- possible_rows
    for(i in 1:(m-1)){
      list_of_lists <- lapply(mat_list,function(x){
        lapply(possible_rows,function(y){
          rbind(x,y)
        })
      })
      mat_list <- Reduce(c,list_of_lists)
      print(c(i,length(mat_list)))
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2018-10-20
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2018-10-26
      • 2014-04-22
      相关资源
      最近更新 更多