How do I create a matrix with all binary combinations?
古いコメントを表示
Hi
I want to create a matrix with all binary combinations. If N is the length of the binary code, there would be
possible combinations. Below are
, and the 16 possible binary combinations are displayed.
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
How can I create such a matrix automatically? I don't really care which order they are in, as long as every combination is present, and N is a variable.
Thanks in advance.
採用された回答
その他の回答 (2 件)
Fangjun Jiang
2020 年 5 月 13 日
I though it should be
ff2n(4)
2 件のコメント
Christian P
2020 年 5 月 13 日
Fangjun Jiang
2020 年 5 月 14 日
ff2n, Two-level full-factorial design, is built for this.
James Tursa
2020 年 5 月 13 日
編集済み: James Tursa
2020 年 5 月 13 日
dec2bin(0:2^N-1) - '0'
Note that this is only practical for relatively small values of N. Even moderatly large N can cause this to exceed your available memory.
5 件のコメント
Pranay Agarwal
2022 年 12 月 1 日
Hi, any similar method is there to extend this code for ternary strings, i.e., to generate all strings containing (0,1,2) of length N
Torsten
2022 年 12 月 1 日
Is this a statement or a question ?
L = 3; % Length
N = 2; % Values in range 0-N
v = (N+1)*ones(1,L);
output = fullfact(v) - 1
N = 4; % length
base = 3;
output = dec2base(0:base^N-1,base)-'0';
disp(output);
Pranay Agarwal
2022 年 12 月 2 日
Thanks a lot....
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!