Repeating and shifting a vector

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Repeating and shifting a vector

hjborsje

Is there a way to create a matrix from a vector, where each new row is either shifted or rotated one element, but if possible without a for-loop?

 

A simple example:

 

From A = [1 2 3 4 5 6 7 8 9 10]

 

Create B = [1 2 3 4 5 6 7 8 9 10 ; 0 1 2 3 4 5 6 7 8 9; 0 0 1 2 3 4 5 6 7 8]

 

This shows the last element dropping off, but a shift would be just as useful.

 

Any ideas will be welcome.

 

Henk Borsje



Reply | Threaded
Open this post in threaded view
|

Re: Repeating and shifting a vector

Nir Krakauer-3
try
B = gallery ("circul", A)


Reply | Threaded
Open this post in threaded view
|

Re: Repeating and shifting a vector

Michele Ginesi
In reply to this post by hjborsje


On 3/5/20 5:33 PM, Henk Borsje wrote:

Is there a way to create a matrix from a vector, where each new row is either shifted or rotated one element, but if possible without a for-loop?

 

A simple example:

 

From A = [1 2 3 4 5 6 7 8 9 10]

 

Create B = [1 2 3 4 5 6 7 8 9 10 ; 0 1 2 3 4 5 6 7 8 9; 0 0 1 2 3 4 5 6 7 8]

 

This shows the last element dropping off, but a shift would be just as useful.

 

Any ideas will be welcome.

 

Henk Borsje



    
You can achieve it using the toeplitz command. Your example can be obtained by:
r = 1:10;
c = [1; 0; 0];
B = toeplitz(c, r)