I have a question about how to generate all possible combinations of an array that satisfy the following condition:
- with fixed length
N
- only
M
elements amongN
are1
, and the rest elements are0
.
For example, N = 4
and M = 2
, we shall have
1: '0011'
2: '0101'
3: '0110'
4: '1001'
5: '1010'
6: '1100'
The basic idea is to pick up M
elements from range(N)
and replace the corresponding indices in np.zeros(N)
with 1
. However, I can only realize this idea by using several for-loops in python, which is inefficient. I'd like to ask whether there is any straightforward solution? Thanks in advance.