Reputation: 23
I have a 5x5
matrix M = magic(5)and I must add two sub-matrices of it (using the
sumcommand) and store it in
G`, 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]));
Upvotes: 1
Views: 109
Reputation: 4336
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);
Upvotes: 3