MATLAB Answers

0

Create matrix indicating combinations of dummy variables

Asked by Zhenyu Yao on 19 Sep 2019 at 1:05
Latest activity Edited by James Tursa
on 19 Sep 2019 at 1:12
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!

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by James Tursa
on 19 Sep 2019 at 1:10
Edited by James Tursa
on 19 Sep 2019 at 1:12
 Accepted Answer

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 Comments

Sign in to comment.