0

How to effectively implement in matlab this specific combinatorics problem?

I have a list of possible values {{v1_1,...v1_n1},{v2_1, ..., v2_n2}, ... {vm_1, ... , vm_nm}} . I need to generate all possible configurations in this fashion:

example of list

{{1,2}, {3}, {2,5}}

and corresponding configurations are

  1 3 2
  1 3 5
  2 3 2
  2 3 5

In my case the max(n1,...,nm) = 6 and m is small integer ( 3 < m < 30)

The list is represented in my code by binary matrix m x 6

example (3 x 6 case): {{1,2}, {3}, {2,5}}

corresponding binary matrix

 1 1 0 0 0 0  ...  {1,2}
 0 0 1 0 0 0  ...  {3}
 0 1 0 0 1 0  ...  {2,5}

Thanks in advance for any help.

michal
  • 239
  • 2
  • 9
  • To summarize the question linked to above: if you have the Neural Network Toolbox, [`combvec`](http://www.mathworks.com/help/nnet/ref/combvec.html) will get the job done. If you don't, I recommend the [`ndgrid`](http://www.mathworks.com/help/matlab/ref/ndgrid.html) technique [outlined by Amro](http://stackoverflow.com/a/4169488/3121310). – TroyHaskin May 22 '15 at 13:56

1 Answers1

0

Like @TroyHaskin said, ndgrid is the way to go. On top of that you just need to do some pre and post processing.

x = {{1,2}, {3}, {2,5}};
a = cellfun(@cell2mat, x, 'UniformOutput', 0);
b = cell(size(a));
[b{:}] = ndgrid(a{:});
c = cellfun(@squeeze, b, 'UniformOutput', 0);
d = cell2mat(cellfun(@(x) reshape(x, [], 1), c,'uni',0));
user1543042
  • 3,422
  • 1
  • 17
  • 31