Finding All Combinations of Elements in a Vector

4 ビュー (過去 30 日間)
Chad
Chad 2020 年 6 月 11 日
コメント済み: Chad 2020 年 6 月 11 日
I want to create all combinations of a 1x6 vector, composed only of 1's and 0's. Starting with all zeros and ending with all ones. If my math is correct there should be 64 combinations.
A = [0 0 0 0 0 0]
A = [1 0 0 0 0 0]
A = [0 1 0 0 0 0]
....
A = [1 1 1 1 1 1]
I plan to run this in a loop, with A being 1 of the 64 combinations in each iteration.

採用された回答

James Tursa
James Tursa 2020 年 6 月 11 日
編集済み: James Tursa 2020 年 6 月 11 日
N = 6;
result = dec2bin((0:(2^N-1))') - '0';
Then the various vectors you want are the rows of result.
Or, if you really want to generate them in a loop, it would be something like
N = 6;
for k=0:(2^N-1)
A = dec2bin(k) - '0';
% use A here
end
  1 件のコメント
Chad
Chad 2020 年 6 月 11 日
Thank you. I ended up stumbling around dec2bin a few minutes ago and got the same answer you provided. I just generated a big matrix A and called the rows individually in the loop.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by