Overlapping non-square block-diagonal matirce

5 次查看(过去 30 天)
I would like to build a block matrice with overlapping non-square blocks.
Example 1:
[1,2,1,0,0,0,0;
0,0,1,2,1,0,0;
0,0,0,0,1,2,1]
Example 2:
X = [1,1,zeros(1,5),1,2,1,zeros(1,5),1,1];
[X, zeros(1,4);
zeros(1,2), X, zeros(1,2);
zeros(1,4), X
Because of sparsity I would prefer to use a sparse matrix.

回答(1 个)

J. Alex Lee
J. Alex Lee 2019-12-3
As long as there are well defined rules for the blocks and their positions in the matrix, direct use of the sparse() function should work well...try this
% the block
blk = [1 2 1]
% size of the block
w = size(blk,2)
h = size(blk,1)
% number of blocks
N = 5
% how much to shift blocks
vshft = 1; % vertical
hshft = 2; % horizontal
% i think this is right...
nRows = vshft*(N-1) + h
nCols = hshft*(N-1) + w
% pre-allocate
B = sparse(nRows,nCols);
% the subscripts of the original block
% these are to be shifted for indexing into the final matrix
% maybe there's a better way to do this
[jdx0,idx0] = meshgrid(1:w,1:h)
for k = 1:N
idx = idx0 + (k-1)*vshft;
jdx = jdx0 + (k-1)*hshft;
% in case you want really overlapping to add overlapping blocks
B = B + sparse(idx(:),jdx(:),blk(:),nRows,nCols);
end
full(B)

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by