-1

i have to turn a list of numbers (ex. 4 1 3 2) into a list of the same numbers but with multiple copies of each number (and they have to be in the specific order) (ex. 4 4 4 4 1 1 1 1 3 3 3 3 2 2 2 2)

right now my plan is to make a 4x1 matrix of each one ( like a = [ 4 4 4 4]) but i'm having trouble making this run for every number in the list. i made a function that takes in the value (4) and makes a matrix of it that contains 4 copies of it in a 4x1 through a loop.

can i make a loop that runs this copy for each number in the list?

aftewrads i think i can use vertcat to combine all the matrices into the list that i'm looking for.

thanks!

  • possible duplicate of [MATLAB: Duplicate each element of a vector?](http://stackoverflow.com/questions/17509090/matlab-duplicate-each-element-of-a-vector) – bla Jul 11 '13 at 00:28
  • and ... http://stackoverflow.com/questions/14576007/how-to-double-the-size-of-a-matrix-and-propagate-its-elements-in-matlab/14576141#14576141 – bla Jul 11 '13 at 00:28
  • and ... http://stackoverflow.com/questions/17522568/matlab-copying-the-rows-n-times-in-order/17522639#comment25479393_17522639 – bla Jul 11 '13 at 00:29
  • and... http://stackoverflow.com/questions/14615305/a-similar-function-to-rs-rep-in-matlab – bla Jul 11 '13 at 00:30
  • and the list goes on... – bla Jul 11 '13 at 00:30

1 Answers1

1

No need to loop in any case.

With constant length for each entry in val = [4 1 3 2], repmat() and reshape():

len = 4;
reshape(repmat(val,len,1),1,[])

or for variable lengths, decode with FEX:rude()

len = [1 2 3 4];
rude(len,val)
Oleg
  • 10,406
  • 3
  • 29
  • 57