MATLAB: All possible combination for picking up from arrays

combinationprogramming

I have 3 arrays namely a,b & c.
Now I have total 10 elements..a has 5 elements, b has 2 and c has 3, all elements in an array are identical.
I have to pick up 4 elements in all possible distinct combination from these arrays, like I can pick up all 4 from array a, again 3 from array a and 1 from array b , 2 from each array b & c and so on.
How to write this logic in matlab so that all possible combination is covered.

Best Answer

One simple way that may be a bit wasteful as it calculate more combinations than necessary and remove redundant ones:
Concatenate your 3 arrays and use nchoosek to pick all combinations of 4 elements from that concatenation. Remove duplicate combinations using unique (with the 'rows' option).
With only ten elements it's near instananeous anyway. With moderately more elements, nchoosek will take too long and generate too many combinations, so a different method will have to be used.