1

我有一个5x5矩阵 M = magic(5) and I must add two sub-matrices of it (using thesum command) and store it inG`,它们是:

M(1:3,1:3)M(3:5,3:5)

我写了这个,但我不确定它是否正确,

G=sum(M([1:3,1:3],[3:5,3:5])); 
4

1 回答 1

3

如评论中所述,您可以使用+.

 M = magic(5);
 A = M(1:3,1:3);
 B = M(3:5,3:5);
 G = A + B;

如果你想使用它可能会变得有点复杂sum

C(:,:,1) = A;
C(:,:,2) = B;
G = sum(C,3);
于 2014-11-13T14:59:44.320 回答