Create All Possible Combiations of 0,1, or 2 "1"S of a Binary Vector of Length N

Create all possible combiations of 0,1, or 2 1s of a binary vector of length n

This algorithm might be be more effective than that based on expand.grid:

n <- 3
z <- rep(0,n)

answer <- t(apply(combn(0:n,2),2,function(k) {z[k]=1;z}))
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 1 0
# [3,] 0 0 1
# [4,] 1 1 0
# [5,] 1 0 1
# [6,] 0 1 1

[EDIT] I noticed that my original solution misses a trivial case of all zeros,
which can be easily fixed:

rbind(unname(z),answer)
# [,1] [,2] [,3] [,4]
# [1,] 0 0 0 0
# [2,] 1 0 0 0
# [3,] 0 1 0 0
# [4,] 0 0 1 0
# [5,] 0 0 0 1
# [6,] 1 1 0 0
# [7,] 1 0 1 0
# [8,] 1 0 0 1
# [9,] 0 1 1 0
# [10,] 0 1 0 1
# [11,] 0 0 1 1

Generate all possible binary vectors of length n in R

n = 3
expand.grid(replicate(n, 0:1, simplify = FALSE))
# Var1 Var2 Var3
#1 0 0 0
#2 1 0 0
#3 0 1 0
#4 1 1 0
#5 0 0 1
#6 1 0 1
#7 0 1 1
#8 1 1 1

R: all possible combinations of a binary vector of length n, where only one value is “1” in each combination of m

This is very similar to your other question. In my answer to that question, we see that rephrasing the question, makes it much easier to attack. So for this question, we can reduce it to: "How to generate all pairwise permutations of powers of 2 with repeats?"

We can use almost exactly the same setup as before, only this time we set the argument 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

This function generalizes to any number of bits as well as any number of groups. For example, all possible 3-tuples of 3 bits is given by:

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

And here are the last 9 groups:

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

If you need the output in a list, try this:

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

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

How to get all combination of n binary value?

Use itertools.product

import itertools
lst = list(itertools.product([0, 1], repeat=3))

This will yield a list of tuples (see here)

You can easily change this to use a variable repeat:

n = 3
lst = list(itertools.product([0, 1], repeat=n))

If you need a list of lists, then you can use the map function (thanks @Aesthete).

lst = map(list, itertools.product([0, 1], repeat=n))

Or in Python 3:

lst = list(map(list, itertools.product([0, 1], repeat=n)))
# OR
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

Note that using map or a list comprehension means you don't need to convert the product into a list, as it will iterate through the itertools.product object and produce a list.

generate all combinations of 0's and 1's for a given length with min and max 1's given

There's a simple algorithm for iterating combinations of size n with k ones:

  1. Start with a bit vector of length n, of which the last k bits are 1.
  2. Repeat as long as possible (i.e. until you get a bit vector of length n of which the first k bits are 1):
    a. Find the last 01 sequence in the bit vector. Change it to 10 and move all the following 1 bits (which must be immediately following) to the end of the sequence.

There's a simple loop-free bit-manipulation hack to do that. You can see it in my answer to this question: Find n-th set of a powerset

Algorithm to generate all possible arrays of ones and zeros of a given length

How would you count manually on paper? You would check the last digit. If it is 0, you set it to 1. If it is already 1, you set it back to 0 and continue with the next digit. So it's a recursive process.

The following program generates all possible combinations by mutating a sequence:

#include <iostream>

template <typename Iter>
bool next(Iter begin, Iter end)
{
if (begin == end) // changed all digits
{ // so we are back to zero
return false; // that was the last number
}
--end;
if ((*end & 1) == 0) // even number is treated as zero
{
++*end; // increase to one
return true; // still more numbers to come
}
else // odd number is treated as one
{
--*end; // decrease to zero
return next(begin, end); // RECURSE!
}
}

int main()
{
char test[] = "0000";
do
{
std::cout << test << std::endl;
} while (next(test + 0, test + 4));
}

The program works with any sequence of any type. If you need all possible combinations at the same time, just put them into a collection instead of printing them out. Of course you need a different element type, because you cannot put C arrays into a vector. Let's use a vector of strings:

#include <string>
#include <vector>

int main()
{
std::vector<std::string> combinations;
std::string test = "0000";
do
{
combinations.push_back(test);
} while (next(test.begin(), test.end()));
// now the vector contains all pssible combinations
}

If you don't like recursion, here is an equivalent iterative solution:

template <typename Iter>
bool next(Iter begin, Iter end)
{
while (begin != end) // we're not done yet
{
--end;
if ((*end & 1) == 0) // even number is treated as zero
{
++*end; // increase to one
return true; // still more numbers to come
}
else // odd number is treated as one
{
--*end; // decrease to zero and loop
}
}
return false; // that was the last number
}

Generate list of all possible combinations of elements of vector

You're looking for expand.grid.

expand.grid(0:1, 0:1, 0:1)

Or, for the long case:

n <- 14
l <- rep(list(0:1), n)

expand.grid(l)


Related Topics



Leave a reply



Submit