Combining individual matrices diagonally into bigger matrix with overlapping elements added
Show older comments

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 Comment
Stephen23
on 13 Feb 2015
So the overlap is always only one row/column, never more?
Accepted Answer
More Answers (2)
Eduardo Márquez
on 13 Feb 2015
Edited: Eduardo Márquez
on 13 Feb 2015
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 Comments
Guillaume
on 13 Feb 2015
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
on 13 Feb 2015
Thanks for watching, sorry, it is customary. I'll take it into consideration from now.
RG_85
on 16 Feb 2015
0 votes
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!