Hi Guys,
I need to generate a random matrix of K columns and N rows containing 0 and 1 such that
a) each row contains exactly k ones
b) each row is different from the other (combinatorics imposes that if N > nchoosek(K,k) there will be nchoosek(K,k) rows)
Assume I want N=10000 (out of all the possible nchoosek(K,k)=27405 combinations) different 1 x K (with K=30), vectors containing k (with k=4) ones and Kk zeros .
This code
clear all; close
N=10000; K=30; k=4;
M=randi([0 1],N,K);
plot(sum(M,2)) % condition a) not satisfied
does not satisfy neither a) nor b).
This code
clear all; close;
N=10000;
NN=N; K=30; k=4;
tempM=zeros(NN,K);
for ii=1:NN
ttmodel=tempM(ii,:);
ttmodel(randsample(K,k,false))=1; %satisfies condition a)
tempM(ii,:)=ttmodel;
end
Check=bi2de(tempM); %from binary to decimal
[tresh1,ind,tresh2] = unique(Check);%drop the vectors that appear more than once in the matrix
M=tempM(ind,:); %and satisfies condition b)
plot(sum(M,2)) %verify that condition a) is satisfied
%Effective draws, Wanted draws, Number of possible combinations to draw from
[sum(sum(M,2)==k) N nchoosek(K,k) ]
satisfies condition a) and partially condition b) I say partially because unless NN>>N the final matrix will contain less than N rows each different from each other
Is there a better and faster way (that possible avoids the for cycle and the need of having NN>>N) to solve the problem?
Thanks a lot,
Antonio
