问题
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