Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended block diagonal matrix in Matlab

I know that to generate a block-diagonal matrix in Matlab the command blkdiag generates such a matrix:

enter image description here

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:

enter image description here

  • I guess this can be hardcoded with loops, but I would like to find a more elegant solution. Any ideas on how to implement such a thing?

P.S. I diag command, that using diag(A,k) returns the kth diagonal. I need something for writing in the matrix, for k>0, and for block matrices, not only elements.

like image 685
titus.andronicus Avatar asked Dec 24 '22 22:12

titus.andronicus


1 Answers

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
like image 70
knedlsepp Avatar answered Dec 30 '22 07:12

knedlsepp