I know that to generate a block-diagonal matrix in Matlab the command blkdiag
generates such a matrix:
Now I am faced with generating the same block-diagonal matrix, but with also matrix elements B_1
, B_2
,..., B_{n-1}
on the upper diagonal, zeros elsewhere:
P.S. I diag
command, that using diag(A,k)
returns the k
th diagonal. I need something for writing in the matrix, for k
>0, and for block matrices, not only elements.
There is a submission on the File Exchange that can do this: (Block) tri-diagonal matrices.
You provide the function with three 3D-arrays, each layer of the 3D array represents a block of the main, sub- or superdiagonal. (Which means that the blocks will have to be of the same size.) The result will be a sparse matrix, so it should be rather efficient in terms of memory.
An example usage would be:
As = bsxfun(@times,ones(3),permute(1:3,[3,1,2]));
Bs = bsxfun(@times,ones(3),permute(10:11,[3,1,2]));
M = blktridiag(As, zeros(size(Bs)), Bs);
where full(M)
gives you:
1 1 1 10 10 10 0 0 0
1 1 1 10 10 10 0 0 0
1 1 1 10 10 10 0 0 0
0 0 0 2 2 2 11 11 11
0 0 0 2 2 2 11 11 11
0 0 0 2 2 2 11 11 11
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With