To generate a tridiagonal matrix with replicated elements down the diagonals. Sometimes its nice to generate block tridiagonal matrices of the same form.
full(blktridiag(2,-1,-1,5))
ans =
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
full(blktridiag(ones(2),2*ones(2),3*ones(2),3))
ans =
1 1 3 3 0 0
1 1 3 3 0 0
2 2 1 1 3 3
2 2 1 1 3 3
0 0 2 2 1 1
0 0 2 2 1 1
This can of course be easily extended to handle general blocks, so since I saw an interest from a user, this release does that too.
Amd = reshape(1:16,[2,2,4]);
Asub = reshape(101:112,[2,2,3]);
Asup = reshape(201:212,[2,2,3]);
A = blktridiag(Amd,Asub,Asup);
full(A)
ans =
1 3 201 203 0 0 0 0
2 4 202 204 0 0 0 0
101 103 5 7 205 207 0 0
102 104 6 8 206 208 0 0
0 0 105 107 9 11 209 211
0 0 106 108 10 12 210 212
0 0 0 0 109 111 13 15
0 0 0 0 110 112 14 16
I've also seen a request to allow non-square blocks. This too is now accommodated in the latest release.