Overlapping non-square block-diagonal matirce

8 vues (au cours des 30 derniers jours)
Justin Kant
Justin Kant le 3 Déc 2019
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.

Réponses (1)

J. Alex Lee
J. Alex Lee le 3 Déc 2019
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)

Catégories

En savoir plus sur General Applications dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by