create consecutive duplicates of rows

1 view (last 30 days)
I have a vector A, for example (1:1:5)' I want to create a new vector B in which each row from vector A is repeated, for example 5 times. So A is [1 2 3 4 5]' and B is [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5]'
I have been able to accomplish this with the for loop below, but this takes much too long as my real vector A is over 2 million rows long. Is there a more computationally efficient way to accomplish this?
numbreps=5; for k=1:size(A); B(k*numbreps:k*numbreps+numbreps-1,1)=A(k,1); end; B(1:numbreps-1)=[];

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 15 Oct 2013
a=(1:5)'
b=repmat(a',5,1)
b=b(:)

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!