The N is input.
I want to write a code which gives array of dimension (combin X N) where combin is all possible combination such that sum of each row is N/2.
Confusion? Let me explain with an example…
Let N = 4, I want output array of the following form
[0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0]
note that the sum of each row if N/2 = 2. Let's see another example…
if N=6, output should be
[0 0 0 1 1 10 0 1 0 1 10 0 1 1 0 10 0 1 1 1 00 1 0 0 1 10 1 0 1 0 1..... so on...]
I have a code but it is very inefficient (and also it does not work for N>18 (also it's very slow)
A = dec2bin( 1:2^N-1)-'0';required_array = A(sum(A,2)==N/2,:);
Best Answer