Reputation: 61259
Is there an efficient way to produce square multi-diagonal matricies such as these:
[[1,2,3],
[2,1,2],
[3,2,1]]
[[1,2,3,4,5],
[2,1,2,3,4],
[3,2,1,2,3],
[4,3,2,1,2],
[5,4,3,2,1]]
My efforts so far have produced the following:
t=10
sum=zeros(t,t)
for i=1:t
sum+=diag(ones(1,i)*(t-i)+1,t-i);
end
sum
sum+sum'-diag(ones(1,10),0)
Upvotes: 2
Views: 1137
Reputation: 20915
The command toeplitz
does exactly what you want:
toeplitz([1,2,3,4,5,6])
ans =
1 2 3 4 5 6
2 1 2 3 4 5
3 2 1 2 3 4
4 3 2 1 2 3
5 4 3 2 1 2
6 5 4 3 2 1
Upvotes: 6