Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Random matrix of 0s and 1s with two not trivial constraints
Replies: 1   Last Post: Jun 23, 2013 4:54 AM

 Messages: [ Previous | Next ]
 Antonio Gargano Posts: 1 Registered: 6/22/13
Random matrix of 0s and 1s with two not trivial constraints
Posted: Jun 22, 2013 1:17 PM

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 K-k 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

Date Subject Author
6/22/13 Antonio Gargano
6/23/13 Bruno Luong