All possible combinations in a binary image

I’m trying to create all possible combinations of 0 and 1 in an array that have the shape (n, 10). For example, if we assume an arbitrary combination like this: np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0]), how can I generate all possible combinations (which will result in 2^10=1024 arrays)?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes, you can use itertools.product() with the repeat parameter to generate the desired output:

import numpy as np
from itertools import product

np.array(list(product([0, 1], repeat=10)))

This outputs:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 1]
 [0 0 0 ... 0 1 0]
 ...
 [1 1 1 ... 1 0 1]
 [1 1 1 ... 1 1 0]
 [1 1 1 ... 1 1 1]]

Method 2

You can use permutations from the itertools module:

import numpy as np 
import itertools

list = np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0])
combs = itertools.permutations(list)

for i in combs:
    print(i)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x