Summation of two matrices

3 views (last 30 days)
Dominik Jerinic
Dominik Jerinic on 13 Nov 2021
Commented: Dominik Jerinic on 13 Nov 2021
Hi guys,
I really need your help with the summation of matrices in Matlab.
I have two matrices (180,360), which counts a total of 129 600 values, with different numbers. I would like to sum all the elements within this two matrices as follows:
  • value from the 1st row and 1st coloumn of the 1st matrix sum with the value from the 1st row and 1st coloumn of the 2nd matrix,
  • value from the 1st row and 2nd coloumn of the 1st matrix sum with the value of from the 1st row and 2nd coloum of the 2nd matrix,
  • and so on until the end.
I tried to do next:
Z = A + B and I get only one value in the last row and last coloumn. All the others values are zero (0) even the values in the both matrices are NOT zero (0). I also tried to use funtion SUM like Z = sum (A + B) but I got the result equel to 2 x Z in the last row and last coloumn.
Is there any other (easy) trick to sum this matrices?
Thank you in advance.
  4 Comments
Dave B
Dave B on 13 Nov 2021
Edited: Dave B on 13 Nov 2021
Maybe I didn't understand the problem. one of these matrices has a bunch of zeros on the sides and a bunch of values in the middle, the other has the opposite pattern. They have one column where they overlap. When I add them I see their summed values
load Izb_hv_p
load Izb_hv_bl
Z=Izb_hv_bl+Izb_hv_p;
% Look at some random rows/columns:
for i = 1:10
r=randi(180);
c=randi(360);
fprintf('Izb_hv_p: %0.2f + Izb_hv_bl: %0.2f = Z: %0.2f\n', Izb_hv_p(r,c), Izb_hv_bl(r,c), Z(r,c))
end
Izb_hv_p: 4.05 + Izb_hv_bl: 0.00 = Z: 4.05 Izb_hv_p: 4.21 + Izb_hv_bl: 0.00 = Z: 4.21 Izb_hv_p: 0.00 + Izb_hv_bl: 1.40 = Z: 1.40 Izb_hv_p: 4.49 + Izb_hv_bl: 0.00 = Z: 4.49 Izb_hv_p: 3.65 + Izb_hv_bl: 0.00 = Z: 3.65 Izb_hv_p: 1.39 + Izb_hv_bl: 0.00 = Z: 1.39 Izb_hv_p: 1.53 + Izb_hv_bl: 0.00 = Z: 1.53 Izb_hv_p: 0.00 + Izb_hv_bl: 1.39 = Z: 1.39 Izb_hv_p: 0.00 + Izb_hv_bl: 2.74 = Z: 2.74 Izb_hv_p: 3.76 + Izb_hv_bl: 0.00 = Z: 3.76
% Visualize the whole thing as an image:
t=tiledlayout(3,2);
nexttile
imagesc(Izb_hv_bl)
title('Izb_hv_bl','Interpreter','none')
nexttile
imagesc(Izb_hv_p)
title('Izb_hv_p','Interpreter','none')
nexttile([2 2])
imagesc(Z)
title('Z')
set(t.Children,'CLim',[0 max(Z,[],'all')])
colormap turbo
Dominik Jerinic
Dominik Jerinic on 13 Nov 2021
OK, I find out what was wrong in my calculation.
I had (j,i) in the brackets after Izb_hv_bl and Izb_hv_p. Now I deleted this brackets and everything seems to sum right.
I will check that one more time but I thing it will work fine.
Thank you for your help and your time.
Best regards mate.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!