Solving permutation/combination example with MATLAB

4 ビュー (過去 30 日間)
Dave
Dave 2014 年 2 月 15 日
コメント済み: Dave 2014 年 2 月 18 日
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.

採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 15 日
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 件のコメント
Dave
Dave 2014 年 2 月 18 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by