Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random matrix of 0s and 1s with two not trivial constraints

Subject: Random matrix of 0s and 1s with two not trivial constraints

From: Antonio Gargano

Date: 22 Jun, 2013 17:17:20

Message: 1 of 2

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

Subject: Random matrix of 0s and 1s with two not trivial constraints

From: Bruno Luong

Date: 23 Jun, 2013 08:54:11

Message: 2 of 2

K = 30;
k = 4;
N = 10000;
C = nchoosek(K,k);
if N > C
    error('not possible')
end
itab = randperm(C,N);

J = nchoosek(1:K,k);
J = J(itab,:);

I = repmat((1:N)', [1 k]);
a = accumarray([I(:) J(:)], 1);

% Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us