Combining individual matrices diagonally into bigger matrix with overlapping elements added
Afficher commentaires plus anciens

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 commentaire
Stephen23
le 13 Fév 2015
So the overlap is always only one row/column, never more?
Réponse acceptée
Plus de réponses (2)
Eduardo Márquez
le 13 Fév 2015
Modifié(e) : Eduardo Márquez
le 13 Fév 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 commentaires
Guillaume
le 13 Fév 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
le 13 Fév 2015
Thanks for watching, sorry, it is customary. I'll take it into consideration from now.
RG_85
le 16 Fév 2015
0 votes
Catégories
En savoir plus sur Sparse Matrices dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!