Combining individual matrices diagonally into bigger matrix with overlapping elements added
이전 댓글 표시

Hello all,
I'm throwing in the towel after struggling to come up with any elegant way to do a problem. I've uploaded the input and desired output as an image.
In words, basically I have 4qty 2x2 matrices that need to go into a bigger 5x5 matrix such that the overlapping elements are added. The bigger matrix size is a square matrix, always 1 plus the size of the smaller matrices. All the smaller matrices are the same size, in this case 2x2.
The issue is that the code needs to work for other cases too , in the sense that if I have 6qty of 2x2 matrices, the bigger 7x7 matrix also needs to have the sums of overlapping elements. So I'm looking for a general code.
Thanks really for helping!
댓글 수: 1
Stephen23
2015년 2월 13일
So the overlap is always only one row/column, never more?
채택된 답변
추가 답변 (2개)
Eduardo Márquez
2015년 2월 13일
편집: Eduardo Márquez
2015년 2월 13일
Maybe this:
Matrixs = ones(2,2,4);
d = size(Matrixs);
final = zeros(d(1)*d(3) - (d(3)-1),d(2)*d(3) - (d(3)-1));
for k = 1:d(3)
final(1+((k-1)*(d(1)-1)):1+((k-1)*(d(1)-1))+d(1) - 1,1+((k-1)*(d(2)-1)):1+((k-1)*(d(2)-1))+d(2) -1)=Matrixs(:,:,k)+...
final(1+((k-1)*(d(1)-1)):1+((k-1)*(d(1)-1))+d(1) - 1,1+((k-1)*(d(2)-1)):1+((k-1)*(d(2)-1))+d(2) -1) %;
end
If change dims of Matrixs works, include if matrix are rectangular.
댓글 수: 2
Guillaume
2015년 2월 13일
Eduardo,
I would avoid writing clear all; close all; clc in answers. Leave my workspace, figures and command window alone. Your answer should work regardless of their state.
Eduardo Márquez
2015년 2월 13일
Thanks for watching, sorry, it is customary. I'll take it into consideration from now.
카테고리
도움말 센터 및 File Exchange에서 Sparse Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!