如 m=matrix(1 2 3, 4 5 6);
m -> 21
有2种方法,方法1,先用sum得到各列的和,再调用sum得到所有的元素的和:
sum(sum(m))
方法2,先用 flatten 把矩阵拉成向量,再算 sum:
sum(m.flatten())