MATLAB: Repeating a row vector in MATLAB until it reaches a specified length.

repeatingrow

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,

Best Answer

Here is one way to do it
k_new = k(mod(0:length(j)-1,length(k))+1)
HTH