Since the length of Matches
may vary, you need to use comma-separated lists, together with ndgrid
to generate all combinations (the approach is similar to that used in this other answer). Then filter out combinations where the indices are not increasing, using diff
and logical indexing:
cc = cell(1,numel(Match)); %// pre-shape to be used for ndgrid output
[cc{end:-1:1}] = ndgrid(Match{end:-1:1}); %// output is a comma-separated list
cc = cellfun(@(v) v(:), cc, 'uni', 0) %// linearize each cell
combs = [cc{:}]; %// concatenate into a matrix
ind = all(diff(combs.')>0); %'// index of wanted combinations
combs = combs(ind,:); %// remove unwanted combinations
The desired result is in the variable combs
. In your example,
combs =
1 3 7
1 5 7
2 3 7
2 5 7