How can I create in Matlab a matrix (20 X 12) with random distribution of numbers 1 and 0, when in each column I must have 40% of numbers 1 and 60% of numbers 0? this must be random distribution.
Anyone could help me?
Thanks a ton!
An efficient method is:
This can be done easily with prctile and bsxfun:
rows = 20;
cols = 12;
p = 40; %// percent of 1 values
A = rand(rows,cols); %// uniform random values between 0 and 1
perc = prctile(A,p); %// percentile of each column
A = bsxfun(@le, A, perc); %// 1 if lower or equal than percentile, 0 otherwise
Here is a robust method, set the first 40% of each column to be 1 and then just randomly reorder each column.
m=20;
n=12;
M = zeros(m,n);
M(1:round(m*0.4),:) = 1;
for col = 1:n
M(:,col) = M(randperm(m), col);
end
This could be one "aggressive" approach -
N = 10000;%%// A big number to choose 12 columns from
A = round(rand(20,N));
out = A(:,find(sum(A,1)==round(0.4*size(A,1)),12))
Let us know if this works for you!