MATLAB: How to generate an array that satisfies a given number of repetitions and unique values

generate arrayhistogramrun length encoding

I have a repetition vector x = [3 2 1 2] and set of unique values y = [1 2 3 4]; How can I generate a vector z such that x = histc(z,y);
In this case z would be [1 1 1 2 2 3 4 4]
Thanks in advance!
KL

Best Answer

Edit
x = [3 2 1 2]
y = [1 2 3 4]
z=arrayfun(@(ii,jj) repmat(jj,1,ii),x,y,'un',0)