MATLAB Answers

Overlapping non-square block-diagonal matirce

1 view (last 30 days)
Justin Kant
Justin Kant on 3 Dec 2019
Answered: J. Alex Lee on 3 Dec 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.

  0 Comments

Sign in to comment.

Answers (1)

J. Alex Lee
J. Alex Lee on 3 Dec 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)

  0 Comments

Sign in to comment.

Sign in to answer this question.