Merge two or more matrices while overlapping them

5 views (last 30 days)
I want to merge two matrices like shown below. Where, overlapping cells are added together to create a bigger matrix.
The columns and rows of A and B, have a differnent "global ID". When they are combined together, whichever cells have overlapping ID, are summed up.

Accepted Answer

Image Analyst
Image Analyst on 9 Dec 2020
Assuming they're double arrays rather than cell arrays, try this:
A = [a, b; c, d]
B = [1, 2; 3, 4];
D = zeros(3, 3);
D(1:2, 1:2) = A; % Put A in
% Now add in B
D(2:end, 2:end) = D(2:end, 2:end) + B

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!