MATLAB: Replication of elements in a vector

index vectorreplication

There's a data set
x = [1 2.2 1.5 4 2]
Consider a index vector
indx = [0 0 2 1 2]
The number of copies of each element in 'x' is given in 'indx.' I want a new vector in which the elements of x are arranged according to indx, i.e., the output should be
xnew = [1.5 1.5 4 2 2]
Could you please suggest ways to do this without a for loop?

Best Answer

repelem(x,indx)