Adding two submatrices in Matlab?

119 views Asked by At

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:

M(1:3,1:3) and 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])); 
1

There are 1 answers

0
Rash On BEST ANSWER

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,

C(:,:,1) = A;
C(:,:,2) = B;
G = sum(C,3);