How to create N-tuples in Matlab?
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pontus Vikstål
am 29 Sep. 2018
Beantwortet: Steven Lord
am 5 Jan. 2024
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))
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 29 Sep. 2018
dec2bin(0:2^3-1)-'0'
2 Kommentare
Walter Roberson
am 30 Sep. 2021
A = dec2bin(0:2^3-1)
dec2bin() outputs characters.
double(A)
Those characters have internal codes 48 ('0') or 49 ('1')
B = '0'
double(B)
The character code for '0' is 48
A - B
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.
Weitere Antworten (2)
Steven Lord
am 5 Jan. 2024
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)
If you need the result as a numeric array rather than a table array:
c = combinations(v, v, v).Variables
0 Kommentare
Stephan Reichelt
am 5 Jan. 2024
There is another quite simple implementation of this problem:
% define the values
v = [0, 1]
% generate all combinations of length 3 with repetitions
C = combvec(v,v,v)'
1 Kommentar
John D'Errico
am 5 Jan. 2024
Note that combvec is not found in MATLAB, unless you have the neural net toolbox.
which combvec -all
help combvec
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!