Matrix showing all combinations of multiple dice roll
Mostra commenti meno recenti
I want to create a matrix showing all possible combinations of tossing n independent dice rolls, except that dice is not always 6-sided.
e.g. I want to create matrix res
res = [1,1,1;
1,1,2;
1,2,1;
1,2,2;
2,1,1;
2,1,2;
2,2,1;
2,2,2];
using a vector v = [2,2,2]. Basically vector v specifies the number and size of each dice. Is there a fast way to iterate this without using for loop? I know if the dice size is constant I can use:
res = dec2base(0:dice_size^n-1,dice_size) - '0';
res = res + 1;
But I am stuck when each dice has different sizes.
Risposta accettata
Più risposte (2)
Torsten
il 12 Ott 2018
2 voti
https://de.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin
Best wishes
Torsten.
1 Commento
Nicolas Tarino
il 13 Ott 2018
Image Analyst
il 12 Ott 2018
1 voto
ndgrid or meshgrid is what experienced MATLABers would use. For beginners, a set of nested for loops would work if the number of dice was known in advance (like 3) and not a variable (like n=3).
1 Commento
Nicolas Tarino
il 13 Ott 2018
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!