How to create N-tuples in Matlab?

22 ビュー (過去 30 日間)
Pontus Vikstål
Pontus Vikstål 2018 年 9 月 29 日
回答済み: Steven Lord 2024 年 1 月 5 日
What would be the easiest way to create a list of n-tuples in Matlab?
For example, if I want to generate all possible 3-tuples of 0 and 1: I'd want to generate the following set of tuples:
((0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0),(1,0,1),(1,1,0),(1,1,1))

採用された回答

Bruno Luong
Bruno Luong 2018 年 9 月 29 日
dec2bin(0:2^3-1)-'0'
  2 件のコメント
Andrew Dabrowski
Andrew Dabrowski 2021 年 9 月 30 日
What does the "-'0'" do?
Walter Roberson
Walter Roberson 2021 年 9 月 30 日
A = dec2bin(0:2^3-1)
A = 8×3 char array
'000' '001' '010' '011' '100' '101' '110' '111'
dec2bin() outputs characters.
double(A)
ans = 8×3
48 48 48 48 48 49 48 49 48 48 49 49 49 48 48 49 48 49 49 49 48 49 49 49
Those characters have internal codes 48 ('0') or 49 ('1')
B = '0'
B = '0'
double(B)
ans = 48
The character code for '0' is 48
A - B
ans = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
subtract the character code for '0' from the character codes for the bits to get relative offsets compared to '0' . Characters that were '0', upon having '0' subtracted, give a result of (numeric) 0 . Characters that were '1', upon having '1' subtracted, give a result of (numeric) 1 because the internal code for the character '1' is (numeric) 1 greater than the internal code for the character '0'
So subtracting '0 converts between characters '0' and '1' to numeric 0 and 1.

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2024 年 1 月 5 日
If you're using release R2023a or later of MATLAB you could use the combinations function to do this.
v = [0 1];
c = combinations(v, v, v)
c = 8×3 table
v v_1 v_2 _ ___ ___ 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
If you need the result as a numeric array rather than a table array:
c = combinations(v, v, v).Variables
c = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

Stephan Reichelt
Stephan Reichelt 2024 年 1 月 5 日
There is another quite simple implementation of this problem:
% define the values
v = [0, 1]
v = 1×2
0 1
% generate all combinations of length 3 with repetitions
C = combvec(v,v,v)'
C = 8×3
0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 1
  1 件のコメント
John D'Errico
John D'Errico 2024 年 1 月 5 日
Note that combvec is not found in MATLAB, unless you have the neural net toolbox.
which combvec -all
/MATLAB/toolbox/nnet/nnet/nndatafun/combvec.m
help combvec
COMBVEC Create all combinations of vectors. combvec(A1,A2,...) takes any number of inputs A, where each Ai has Ni columns, and return a matrix of (N1*N2*...) column vectors, where the columns consist of all combinations found by combining one column vector from each Ai. For instance, here the four combinations of two 2-column matrices are found. a1 = [1 2 3; 4 5 6]; a2 = [7 8; 9 10]; a3 = combvec(a1,a2) Documentation for combvec doc combvec

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by