2

The vector should look something like,

[1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3]; 

I know the vector ([1 2 3]) that I wish to 'paste' along the diagonal but I do not know the size of the array so the number of rows would need to be determined by a variable N.

0

3 Answers 3

4

You can use spdiags to set the diagonals and have the desired shape:

n = 4; A = full(spdiags(ones(n,1)*[1,2,3],[0,1,2],n,n+2)); 

This returns:

A = 1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 
Sign up to request clarification or add additional context in comments.

Comments

4

It's a bit crude, but possible to construct the desired matrix as toeplitz:

a = [1 2 3]; toeplitz([a(1); zeros(length(a),1)],[a(:); zeros(length(a),1)]) 

with answer:

ans = 1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 

Comments

2

You can also use 2D-convolution:

v = [1 2 3]; N = 4; result = conv2(v, eye(N)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.