how to build all binary vectors of length n whose number of its zero elements is m<n?
1 回表示 (過去 30 日間)
古いコメントを表示
how to build all binary vectors of length n whose number of its zero elements is m<n?
0 件のコメント
回答 (2 件)
Image Analyst
2013 年 6 月 29 日
If you specify m, and want all binary vectors with length longer than m, then there is an infinite number of vectors satisfying that. For example
m = 4;
output = false(1, 7) % This one has length 7.
output = false(1, 42) % This one has length 42.
0 件のコメント
Roger Stafford
2013 年 6 月 29 日
編集済み: Roger Stafford
2013 年 6 月 29 日
N = 0;
for k = 0:m
N = N + nchoosek(n,k);
end
A = ones(N,n);
q = 1;
for k = 1:m
p = nchoosek(1:n,k);
for r = 1:size(p,1)
q = q+1;
A(q,p(r,:)) = 0;
end
end
The n-element rows of array A have all possible combinations of ones and zeros in which there are no more than m zeros. I assumed here that m and n were fixed quantities which you specify.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!