MATLAB: Matrix showing all combinations of multiple dice roll

iterationMATLABmatrix array

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.

Best Answer

dice_size=[2 3 4];
n = length(dice_size);
c = arrayfun(@(n) 1:n, flip(dice_size), 'unif', 0);
[c{:}] = ndgrid(c{:});
c = cat(n+1,c{:});
c = fliplr(reshape(c,[],n))
c =
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
1 2 2
1 2 3
1 2 4
1 3 1
1 3 2
1 3 3
1 3 4
2 1 1
2 1 2
2 1 3
2 1 4
2 2 1
2 2 2
2 2 3
2 2 4
2 3 1
2 3 2
2 3 3
2 3 4