R:长度为n的二进制向量的所有可能组合,其中在m的每个组合中只有一个值为“1”

时间:2018-01-18 05:31:20

标签: r combinations combinatorics

在R中是否有一种方法可以创建m的二进制集,其中包含n个列的所有组合,而按行只有 1的值可以是&#34; 1&#34;?< / p>

例如,对于n = 2和m = 2,我们将分别具有m的以下组合:

.affix{
 top:60px;
}
.affix-bottom {
 bottom:270px;
 position: absolute;
}

但是,例如,这些允许:     (11,00),(01,11),(00,11),(11,10),(11,11)

1 个答案:

答案 0 :(得分:2)

这与您的其他question非常相似。在这个问题的my answer中,我们看到重新解释这个问题,使攻击更加容易。因此,对于这个问题,我们可以将其缩小为:&#34;如何生成2 的所有成对排列的重复?&#34;

我们可以使用几乎完全相同的设置,只有这次我们设置参数repeats.allowed = TRUE

library(gtools)
bitPairwise2 <- function(numBits, groupSize) {
  t(sapply(t(permutations(numBits + 1, groupSize, 
                           c(0, 2^(0:(numBits-1))), repeats.allowed = TRUE)), 
           function(x) {as.integer(intToBits(x))})[1:numBits, ])
}

bitPairwise2(2,2)
      [,1] [,2]
 [1,]    0    0    ## (00,00)
 [2,]    0    0

 [3,]    0    0    ## (00,10)
 [4,]    1    0

 [5,]    0    0    ## (00,01)
 [6,]    0    1

 [7,]    1    0    ## (10,00)
 [8,]    0    0

 [9,]    1    0    ## (10,10)
[10,]    1    0

[11,]    1    0    ## (10,01)
[12,]    0    1

此函数可以推广到任意数量的位以及任意数量的组。例如,所有可能的3位三元组由下式给出:

## first 9 groups
bitPairwise2(3, 3)[1:27, ]
      [,1] [,2] [,3]
 [1,]    0    0    0    ## (000,000,000)
 [2,]    0    0    0
 [3,]    0    0    0

 [4,]    0    0    0    ## (000,000,100)
 [5,]    0    0    0
 [6,]    1    0    0

 [7,]    0    0    0    ## (000,000,010)
 [8,]    0    0    0
 [9,]    0    1    0

[10,]    0    0    0    ## (000,000,001)
[11,]    0    0    0
[12,]    0    0    1

[13,]    0    0    0    ## (000,100,000)
[14,]    1    0    0
[15,]    0    0    0

[16,]    0    0    0    ## (000,100,100)
[17,]    1    0    0
[18,]    1    0    0

[19,]    0    0    0    ## (000,100,010)
[20,]    1    0    0
[21,]    0    1    0

[22,]    0    0    0    ## (000,100,001)
[23,]    1    0    0
[24,]    0    0    1

[25,]    0    0    0    ## (000,010,000)
[26,]    0    1    0
[27,]    0    0    0

以下是最后9组:

a <- bitPairwise2(3, 3)[166:192, ]
row.names(a) <- 166:192
a
    [,1] [,2] [,3]
166    0    0    1    ## (001,100,001)
167    1    0    0
168    0    0    1

169    0    0    1    ## (001,010,000)
170    0    1    0
171    0    0    0

172    0    0    1    ## (001,010,100)
173    0    1    0
174    1    0    0

175    0    0    1    ## (001,010,010)
176    0    1    0
177    0    1    0

178    0    0    1    ## (001,010,001)
179    0    1    0
180    0    0    1

181    0    0    1    ## (001,001,000)
182    0    0    1
183    0    0    0

184    0    0    1    ## (001,001,100)
185    0    0    1
186    1    0    0

187    0    0    1    ## (001,001,010)
188    0    0    1
189    0    1    0

190    0    0    1    ## (001,001,001)
191    0    0    1
192    0    0    1

如果您需要列表中的输出,请尝试以下操作:

test <- bitPairwise2(4, 3)
numGroups <- nrow(test)/3

makeGroupList <- function(mat, nG, groupSize) {
    lapply(1:nG, function(x) {
        s <- groupSize*(x-1) + 1
        e <- s + (groupSize - 1)
        mat[s:e, ]
    })
}

makeGroupList(test, numGroups, 3)
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0

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

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

 .      .    .    .    .
 .      .    .    .    .
 .      .    .    .    .