I have a 5x5 matrix M = magic(5)and I must add two sub-matrices of it (using thesumcommand) and store it inG`, they are:
5x5
and I must add two sub-matrices of it (using the
command) and store it in
M(1:3,1:3) and M(3:5,3:5)
M(1:3,1:3)
M(3:5,3:5)
And I wrote this, but I', not sure if it's correct,
G=sum(M([1:3,1:3],[3:5,3:5]));
As mentioned in comments, you could easily accomplish your goal with +.
+
M = magic(5); A = M(1:3,1:3); B = M(3:5,3:5); G = A + B;
It could get a little complicated if you want to use sum,
sum
C(:,:,1) = A; C(:,:,2) = B; G = sum(C,3);
As mentioned in comments, you could easily accomplish your goal with
+
.It could get a little complicated if you want to use
sum
,