Repeating a row vector in MATLAB until it reaches a specified length.
Show older comments
Suppose I have 2 single row vectors j and k, such that the length of j > the length of k
The problem: I want to make the length of k= the length of j in such a way that the elements in k keep repeating themselves until the length of k = the length of j.
Example:
j=[1,2, 3, 4, 5, 6, 7]; k=[-1,3,4];
After the program runs, it should return:
k=[-1,3,4,-1,3,4,-1];
How can this be accomplished in MATLAB? The more ways there are to implement this program (loops, if statements, logical indexing, etc.) the merrier.
Regards,
Accepted Answer
More Answers (1)
James Tursa
on 11 Dec 2017
result = k(mod(0:numel(j)-1,numel(k))+1);
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!