Matlab中的扩展块对角矩阵 [英] Extended block diagonal matrix in Matlab
问题描述
我知道要在Matlab中生成块对角矩阵,命令blkdiag
会生成这样的矩阵:
I know that to generate a block-diagonal matrix in Matlab the command blkdiag
generates such a matrix:
现在,我面临着生成相同的块对角矩阵的问题,但是在上对角线上还有矩阵元素B_1
,B_2
,...,B_{n-1}
,其他地方为零:
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.我 diag
命令,该命令使用k
个对角线.我需要一些东西来写矩阵,
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.
推荐答案
文件交换上有一个提交可以做到这一点: (阻止)tri-diagonal矩阵.
There is a submission on the File Exchange that can do this: (Block) tri-diagonal matrices.
您为函数提供了三个3D阵列,该3D阵列的每一层代表一个主,次对角或超对角线的块. (这意味着这些块必须具有相同的大小.)结果将是一个稀疏矩阵,因此在内存方面应该相当有效.
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.
用法示例为:
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);
其中full(M)
为您提供:
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
这篇关于Matlab中的扩展块对角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!