2

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?

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103

1 Answers1

2

You can use repmat or repelems, e.g.

 z = repelems(x,[1:4;rep]) 
Hack-R
  • 22,422
  • 14
  • 75
  • 131