Search code examples
matlabvectorrep

Matlab - equivalent of R's rep() with times argument


I am wondering what is the fastest way to achieve in Matlab what in R I would achieve with the rep() function with the times argument, e.g.

v1=1:5;v2=5:1;out=rep(v1,times=v2);out
# 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5

i.e. replicate each element in vector v1 a number of times given by the corresponding element in vector v2. Any thoughts?


Solution

  • You can use repmat or repelems, e.g.

     z = repelems(x,[1:4;rep])