我有一个5x5
矩阵M = magic(5)and I must add two sub-matrices of it (using the
和command) and store it in
G`,它们是:
M(1:3,1:3)
和M(3:5,3:5)
我写了这个,但我',不确定它是否正确,
G=sum(M([1:3,1:3],[3:5,3:5]));
答案 0 :(得分: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);