Suppose you want to have 20 columns ( n=20 ), and your vector a contains the number of desired in each row:
n=20; a= [5 6 1 9 4]; X= zeros(numel(a),n); for k=1:numel(a) rand_order=randperm(n); row_entries=[ones(1,a(k)),zeros(1,na(k))]; row_entries=row_entries(rand_order); X(k,:)=row_entries; end X=boolean(X);
What I am doing is generating me a random ordered array of rand_order indices, and then getting an array that contains the right number of units filled with zero. Change the order of these elements according to rand_order , saving it and converting it to logical. And due to using the for rand_order all the time, it computes again, so it gives you different locations for your output:
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
source share