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.
댓글 수: 0
답변 (1개)
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 General Applications에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!