Create a block diagonal and subdiagonal with a repeating block in Matlab

隐身守侯 提交于 2019-12-11 11:43:18

问题


Suppose I have an n-by-n matrix A and an n-by-n matrix B. I want to create a block matrix C consisting of N blocks of matrix A as a diagonal and (N-1) blocks of matrix B as a subdiagonal below the A diagonal.

This link gives answers for only the block diagonal case, which are all great. Now I want to add a block subdiagonal, which command in Matlab should I use?

Thank you so much for your help.


回答1:


If you know how to create a matrix C with A on its diagonal (of size nN-by-nN), you can also create a matrix D of smaller size (n(N-1)-by-n(N-1)) with B on its diagonal, then you just need to add D at the right sub-matrix of C:

C( (n+1):end, (n+1):end ) = C( (n+1):end, (n+1):end ) + D;

Alternatively, using kron:

C = kron( eye(n), A ) + kron( diag(ones(n-1,1), -1), B );

You might be better off using sparse matrices here

C = kron( speye(n), A ) + kron( spdiag(ones(n-1,1), -1, n, n), B );


来源:https://stackoverflow.com/questions/26612729/create-a-block-diagonal-and-subdiagonal-with-a-repeating-block-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!