Solving permutation/combination example with MATLAB
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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> )
More Answers (0)
See Also
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!