MATLAB: How to repeat an array based on another array

repeat array

Greetings
I am trying to repeat an array based on another array e.g
Repeat element of array A based on the elements of array B
A= [ 1 2 3 4 ]
B= [ 2 3 1 2]
the resulted array should be ( first element of A that is 1 repeated 2 times which is the first element of array B and so on )
C= [ 1 1 2 2 2 3 4 4 ]

Best Answer

Use repelem.
C = repelem(A,B)