简体   繁体   中英

Adding two submatrices in Matlab?

I have a 5x5 matrix M = magic(5) and I must add two sub-matrices of it (using the sum command) 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])); 

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM