摘自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,DIM) sums along the dimension DIM.
S = sum(..., TYPE) specifies the type in which the
sum is performed, and the type of S. Available options are:
'double' - S has class double for any input X
'native' - S has the same class as X
'default' - If X is floating point, that is double or single,
S has the same class as X. If X is not floating point,
S has class double.
S = sum(..., MISSING) specifies how NaN (Not-A-Number) values are
treated. The default is 'includenan':
'includenan' - the sum of a vector containing NaN values is also NaN.
'omitnan' - the sum of a vector containing NaN values
is the sum of all its non-NaN elements. If all
elements are NaN, the result is 0.
Examples:
If X = [0 1 2; 3 4 5]
then sum(X, 1) is [3 5 7] and sum(X, 2) is [3; 12]
If X = int8(1:20) then sum(X) accumulates in double and the
result is double(210) while sum(X,'native') accumulates in
int8, but overflows and saturates to int8(127).
用法(翻译)
sum:元素的和。
S = sum(X) 是向量X的元素的和。如果X是一个矩阵,S是一个行向量(每一列的元素之和组成)。对于一个N*D的数组,sum(X) 沿着第一个非单例的维度进行操作。(??)>>操作之后发现sum(X)=sum(X,1)
S = sum(X,DIM):沿着某个维度来求和。>>sum(x,1)就是按列求和,生成一个行向量;sum(X,2)就是按行求和,生成一个列向量;sum(X,3),这里只要DIM大于等于3,得出来的结果都是X本身,因为平常用到的矩阵都是二维的(三维情况不同)。
S = sum(…, TYPE)将sum的结果专门指定某种格式,可以指定的种类有:
‘double’ - 对任意输入的X,S都是double型
‘native’ - S和X的型完全相同
‘default’ - 如果X是浮点型,那么不管X是双精度还是单精度型,S都和X型相同. 如果X不是浮点型,那么S是double型。
S = sum(…, MISSING)指定了NAN的值怎么运算,默认是includenan。
‘includenan’ - 包含了非数字量的向量的和任然是非数字量。
‘omitnan’ - the sum of a vector containing NaN values
is the sum of all its non-NaN elements. If all
elements are NaN, the result is 0.(没搞懂,有空更正)