sum

Syntax

Description

S = sum(A) returns
the sum of the elements of A along the first array dimension whose
size does not equal 1.

If A is a vector, then sum(A) returns
the sum of the elements.

If A is a matrix, then sum(A) returns
a row vector containing the sum of each column.

If A is a multidimensional array,
then sum(A) operates along the first array dimension
whose size does not equal 1, treating the elements as vectors. This
dimension becomes 1 while the sizes of all other
dimensions remain the same.

S = sum(A,vecdim)
sums the elements of A based on the dimensions specified in the
vector vecdim. For example, if A is a matrix,
then sum(A,[1 2]) is the sum of all elements in
A, since every element of a matrix is contained in the array
slice defined by dimensions 1 and 2.

S = sum(___,nanflag) specifies
whether to include or omit NaN values from the
calculation for any of the previous syntaxes. sum(A,'includenan') includes
all NaN values in the calculation while sum(A,'omitnan') ignores
them.

Sum of Matrix Rows

Sum of Array Slices

Use a vector dimension argument to operate on specific slices of an array.

Create a 3-D array whose elements are 1.

A = ones(4,3,2);

To sum all elements in each page of A, specify the dimensions in which to sum (row and column) using a vector dimension argument. Since both pages are a 4-by-3 matrix of ones, the sum of each page is 12.

S1 = sum(A,[1 2])

S1 =
S1(:,:,1) =
12
S1(:,:,2) =
12

If you slice A along the first dimension, you can sum the elements of the resulting 4 pages, which are each 3-by-2 matrices.

S2 = sum(A,[2 3])

S2 = 4×1
6
6
6
6

Slicing along the second dimension, each page sum is over a 4-by-2 matrix.

S3 = sum(A,[1 3])

S3 = 1×3
8 8 8

To sum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the 'all' option.

vecdim — Vector of dimensionsvector of positive integers

Vector of dimensions, specified as a vector of positive integers. Each
element represents a dimension of the input array. The lengths of the output
in the specified operating dimensions are 1, while the others remain the
same.

Consider a 2-by-3-by-3 input array, A. Then
sum(A,[1 2]) returns a 1-by-1-by-3 array whose
elements are the sums of each page of A.

The order of the additions in the sum operation is
not defined. Therefore, the sum operation on a GPU
array might not return exactly the same answer as the
sum operation on the corresponding MATLAB® numeric array. The difference might be significant when
A is a signed integer type and its product is
accumulated natively.

Distributed ArraysPartition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

The order of the additions in sum operation is not
defined. Therefore, the sum operation on a distributed
array might not return exactly the same answer as the
sum operation on the corresponding MATLAB numeric array. The difference might be significant when
A is a signed integer type and its product is
accumulated natively.