how to generate binary matrix with all combination having equal probability of 1 and 0 ?

I want to generate binary matrix let say 8X1, In which four 0s and four 1s. so total number of such type of possible matrix would be 70, (see, 8Choose4 ).I want each of these one by one(may be with loop). Please help

 Accepted Answer

You mean like this (I hope this wasn't your homework):
for k = 1 : 70
indexes = randperm(8);
array = zeros(1,8);
array(indexes(1:4)) = 1
end

More Answers (2)

This is not the more efficient way
a=[1 1 1 1 0 0 0 0];
b=unique(perms(a),'rows');
out=b(sum(b,2)==4,:)
use Roger Stafford's idea
n = 8;
m = 4;
i0 = nchoosek(1:n,m);
s = size(i0,1);
out = zeros(s,n);
ii = bsxfun(@plus,(i0 - 1)*s,(1:s)');
out(ii) = 1;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!