How to create N-tuples in Matlab?
Show older comments
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))
Accepted Answer
More Answers (2)
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
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 Comment
Note that combvec is not found in MATLAB, unless you have the neural net toolbox.
which combvec -all
help combvec
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!