MATLAB: Create vector with predetermined number of certain elements

vectors

Hi everyone,
I have a problem where I have a set of numbers, eg; [3 5 9] and for each of these numbers an amount of occurences eg. [2 1 5]. Now i would like to create a vector containing these numbers the prespecified amount of times, so for the example the result would be [3 3 5 9 9 9 9 9 ]. Is there an elegant way to do this in MATLAB, that is, vectorized?
Thanks in advance for your input.

Best Answer

Use the repelem funciton:
V = [3 5 9];
N = [2 1 5];
Out = repelem(V, N)
Out =
3 3 5 9 9 9 9 9