最近阅读代码发现一句: sum(3维数组, 常数)
查询matlab帮助文档后:
sum Sum of elements.
S = sum(X) is the sum of the elements of the vector X. If X is a matrix,
S is a row vector with the sum over each column. For N-D arrays,
sum(X) operates along the first non-singleton dimension.S = sum(X,'all') sums all elements of X. S = sum(X,DIM) sums along the dimension DIM. S = sum(X,VECDIM) operates on the dimensions specified in the vector VECDIM. For example, sum(X,[1 2]) operates on the elements contained in the first and second dimensions of X.
即:S = sum(X)是向量X的元素和。如果X是一个矩阵,S是一个行向量,每个列的和。对于N-D数组,sum(X)沿着第一个非单例维度运行。
S = sum(X,VECDIM)作用于向量VECDIM中指定的维数。例如,sum(X,[1 2])作用于X的第一维和第二维中包含的元素。
下面用一个实例来说明:
首先建立一个自己的三维数组:
>> a = zeros(2,3,2);
>> a(:,:,1)= [1 1 1 ;2 2 2];
>> a(

本文详细介绍了MATLAB中sum函数的使用方法,包括基本语法、参数含义及具体应用场景。通过实例演示了如何对多维数组进行求和操作,并解释了不同参数组合下sum函数的行为差异。
最低0.47元/天 解锁文章
1497

被折叠的 条评论
为什么被折叠?



