Create matrix indicating combinations of dummy variables

2 ビュー (過去 30 日間)
Zhenyu Yao
Zhenyu Yao 2019 年 9 月 19 日
編集済み: James Tursa 2019 年 9 月 19 日
I have eight dummy variables (0-1 variables). I need to find all the possible combinations of these variables.
For example, v1-v8 are 0-1 variables and I want to the following result.
[0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0;
0 0 1 0 0 0 0 0;
0 0 0 1 0 0 0 0;
......
1 1 1 1 1 1 1 1]
Therefore, this matrix should be a 256*8 matrix.
I am very new to MATLAB and do not know how to generate matrix using loop. Thanks for help!

採用された回答

James Tursa
James Tursa 2019 年 9 月 19 日
編集済み: James Tursa 2019 年 9 月 19 日
Variations of this technique are often used, but this can exceed your memory if n gets too large:
n = 8;
result = dec2bin(0:2^n-1) - '0'; % the - '0' turns the dec2bin result into a double

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by