0

I have a quick question. Lets say i want to form a matrix with all possible combinations of some acceptable value, eg. lets say i have 3 elements and i the possible values are 0 1 2 so the matrix would be

   0    0   0
   0    0   1
   0    0   2
   0    1   0
   0    1   1
   0    1   2
   0    2   0
   0    2   1
   0    2   2
   .
   .
   .

and so on for all the possible 3^3 combinations.

How can I do this with Matlab for cases that could span between many other possible values (0 1 2 3 4 5 6 7 8...) and for many more columns this time (not only 3).

Thanks.

Yan Song
  • 2,285
  • 4
  • 18
  • 27
  • 2
    Take a look at the function [combinationMatrix](https://www.mathworks.com/matlabcentral/fileexchange/47828-combinationmatrix-m) availabe on MatlabCentral. – Nakini May 03 '18 at 04:48
  • It's not what I asked – Yan Song May 03 '18 at 05:16
  • 1
    Why not? If I understand you correctly, `combinationMatrix([0:2],3)` fits your question quite well. – Hunter Jiang May 03 '18 at 05:55
  • I *know* that a very similar question has been asked within the last 12 months and that the answer was equivalent to `dec2base(0:3^3-1,3)-48`, but i just can't find it. – Leander Moesinger May 03 '18 at 06:12
  • 1
    I think [this answer](https://stackoverflow.com/a/4169488/4221706) which uses the built-in `ndgrid` function should solve your problem. Does it work that way? – hbaderts May 03 '18 at 06:34
  • I take back what I said. The combinationMatrix is a perfect solution. Thanks! @Nakini – Yan Song May 03 '18 at 07:03

0 Answers0