1
A = [1 4 5 2 1 2]

How could I concisely replicate each element n times whilst maintaining the overall order e.g. if n = 3, the desired result would be:

[1 1 1 4 4 4 5 5 5 2 2 2 1 1 1 2 2 2]
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
AnnaSchumann
  • 1,261
  • 10
  • 22

2 Answers2

3

For Matlab R2015a or higher use repelem

n = 3;
u = repelem(A,n)

For older versions use bsxfun

n = 3;
u = bsxfun(@mtimes ,A(:).',ones(n,1))
u = u(:)
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
1

You could do the following:

reshape(repmat(a',[3 1]),[],1)
DaveG
  • 741
  • 6
  • 16