The Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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;
NN=N; K=30; k=4;
for ii=1:NN
ttmodel(randsample(K,k,false))=1; %satisfies condition a)
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,


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.