create consecutive duplicates of rows
1 view (last 30 days)
Show older comments
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)=[];
0 Comments
Accepted Answer
More Answers (0)
See Also
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!