How can I obtain the permutation of 3 binary variables?

5 ビュー (過去 30 日間)
Matteo Masto
Matteo Masto 2021 年 9 月 22 日
コメント済み: Rik 2021 年 9 月 22 日
Hi everyone!
Can anybody tell me how to obtain the classical permutation matrix of binary variables?
for example given n=3 boolean variables A B C:
ABC
000
001
010
011
100
101
110
111
I hope there would be a command for this.
thank you!!

採用された回答

Rik
Rik 2021 年 9 月 22 日
The easiest way to do this is with bin2dec. You can loop from 0 to 2^n-1 to get all combinations. To convert the character array back to a numeric vector you can simply use =='1'.
  4 件のコメント
Matteo Masto
Matteo Masto 2021 年 9 月 22 日
thanks a lot! I had to fix it a bit but now it works :)
n = 3;
H = zeros(2^n,n);
for k=0:2^n-1
x=dec2bin(k,n)
for q = 1:1:n
H(k+1,q) = str2num(x(q));
end
end
Rik
Rik 2021 年 9 月 22 日
str2num is not a fix, it introduces a call to eval, which is completely unnecessary. Comparing to '1' will already convert to a numeric vector. You can also pass a vector to dec2bin:
n=3;
str=dec2bin(0:2^n-1,n);
H= str=='1';
disp(H)
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by