MATLAB: How to create a random integernumbers with conditions

arrayrandi

I just want to create an array at random which size will be 1 by sum([4 3 3]). The array contains 1 at 4 times, 2 at 3 times and 3 at 3 times. The result will be
A=[1 2 1 3 2 3 1 1 2 3], ie, random.
How can I generate it in the smart way?

Best Answer

ix = [4, 3, 3];
A = repelem(1:3, ix);
A(randperm(numel(A)))