Reputation: 21
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.
Upvotes: 2
Views: 89
Reputation: 112659
You can also use 2D-convolution:
v = [1 2 3];
N = 4;
result = conv2(v, eye(N))
Upvotes: 2
Reputation: 4097
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
Upvotes: 4
Reputation: 8401
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
Upvotes: 4