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]
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]
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(:)