Is there a function implemented for calculating urn problems, especially "Categorical distribution" or "Multinomial distribution", in MATLAB 8.0 (R2012b)?

1 view (last 30 days)
I am looking for a function that returns all possible combinations of outcomes of a categorical distribution.
For example giving the possible outcomes "1" and "2" and making 3 draws, I would expect the function to return a matrix:
ans =
1 1 1
1 1 2
1 2 2
2 2 2

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Jan 2013
There is no certain function provided in MATLAB for those calculations, but you can achieve this using the following code:
nballs = 3;
theballs = [1 2];
unique(nchoosek(kron(theballs, ones(1, nballs)), nballs), 'rows')
KRON puts nballs copies of each of the balls from theballs into the "urn", NCHOOSEK chooses nballs of them, and UNIQUE removes draws that have the same quantity of each ball, just different copies of the same ball. Duplicate values in theballs would be tricky and large values for nballs or numel(theballs) would be memory-intensive.
There is also a solution from MATLAB Central that may fit your needs:
Please note that The MathWorks does not guarantee or warrant the use or content of these submissions to the file exchange. Any questions, issues, or complaints should be directed to the contributing author.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!