Solving permutation/combination example with MATLAB

4 views (last 30 days)
Hi all,
I would like to write a MATLAB function to model a problem:
There are eight boxes, each box containing 8 balls labeled 1 to 8. If I draw one ball from each box, what are all possible combinations of 8 balls for each trial?
One easy combination (out of many) is: 1 1 1 1 1 1 1 1 What is the total number of combinations and what are the sequence of numbers for each?
Extending this idea one step further, what if I draw two balls from each box? One random example would be: [1 2] [2 3] [4 5] [7 8] [7 2] [3 6] [3 9] [3 5]
What if drawing three balls, four balls, etc. from each run?
For the limiting case of drawing 8 balls, the total number of combination is one. [1 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8] ....... [1 2 3 4 5 6 7 8]
Any help/hint on how this can be implemented efficiently in MATLAB? Thank you.

Accepted Answer

Jos (10584)
Jos (10584) on 15 Feb 2014
hints
(1) start with 1 box with M balls, drawing n balls from it. This will give you the set S(n). Take a look at NCHOOSEK
(2) when drawing from K boxes, you can treat these boxes independently. To obtain all possible combinations of K sets you have to obtain the cartesian product of these K sets S(n). Take a look at ALLCOMB ( you can download here: <http://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb> )
  1 Comment
Dave
Dave on 18 Feb 2014
Indeed, with these two functions, I was able to create the complete set of solutions to my problem. Also, breaking the problem into two separate pieces helped me visualize the problem. Your feedback is greatly appreciated. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices 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!