repeated value of a vector
Show older comments
Good evening, I have a doubt that I can't solve... If I have a vector, like (1, 5, 15, 2), and I want to create another one that has every single value of the vector repeated for a certain number of times, for example 2, having as output (1, 1, 5, 5, 15, 15, 2, 2), what is the best way to do it?
I thougth of using a for cycle for each value of the vector but I don't know how.
1 Comment
Chien Poon
on 7 Sep 2021
a = [1, 5, 15, 2]; % Your vector
b = repmat(a,2,1); % This creates a copy of the vector in the 2nd dimension
c = reshape(b,[],1)'; % We then reshape the matrix back to a vector
Accepted Answer
More Answers (1)
repelem is perfect for this kind of problem:
x = [1 5 15 2]
repelem(x,2)
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!