MATLAB --- sum function usage
1/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.
If X is floating point, that is double or single, S is
Accumulated natively, that is in the same class as X,
And s has the same class as X. If X is not floating point,
S is accumulated in double and S has class double.
S = sum (x, dim) Sums along the dimension dim.
S = sum (x, 'double') and S = sum (x, dim, 'double') accumulate
S in double and S has class double, even if X is single.
S = sum (x, 'native ') and S = sum (x, dim, 'native') accumulate
S natively and S has the same class as X.
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
Result is double (210) while sum (x, 'native ') accumulates in
Int8, but overflows and saturates to int8 (127 ).
2/
Syntax
B = sum ()
B = sum (A, dim)
Function Description
B = sum ()
Returns the values of different spatial dimensions in the array. If a is a vector, sum (a) returns the element and sum of the vector. If a is a matrix, sum (a) uses the column vector in A as the base vector and returns the row vectors of each element of the base vector of each column. If a is a multi-dimensional array, sum (a) uses the data along the first non-unit as the vector and returns an array of row vectors.
B = sum (A, dim)
Sums the elements of matrix A along the dimension specified by the scalar dim.
3/
>
A =
3 4 2
1 5 3
4 7 1
> Sum ()
Ans =
8 16 6
> Sum (A, 2)
Ans =
9
9
12
> Sum (A, 1)
Ans =
8 16 6
> A (:, 2)
Ans =
4
5
7