Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave / Matlab: Extend a vector making it repeat itself?

Is there a way to extend a vector by making it repeat itself?

>v = [1 2]; >v10 = v x 5; %x represents some function. Something like "1 2" x 5 in perl 

Then v10 would be:

>v10      1 2 1 2 1 2 1 2 1 2 

This should work for the general case, not just for [1 2]

like image 657
Tom Avatar asked Mar 17 '10 04:03

Tom


People also ask

How do you repeat a row vector in Matlab?

B = repmat( A , r ) specifies the repetition scheme with row vector r . For example, repmat(A,[2 3]) returns the same result as repmat(A,2,3) .


2 Answers

Obviously repmat is the way to go if you know in which direction you want to expand the vector.

However, if you want a general solution that always repeats the vector in the longest direction, this combination of repmat and indexing should do the trick:

 v10=v(repmat(1:length(v),1,5)) 
like image 38
Dennis Jaheruddin Avatar answered Sep 30 '22 17:09

Dennis Jaheruddin


The function you're looking for is repmat().

v10 = repmat(v, 1, 5) 
like image 161
Andrew Shepherd Avatar answered Sep 30 '22 16:09

Andrew Shepherd