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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by