Main Content

sum

Sum of array elements

Description

example

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

  • IfAis a vector, thensum(A)returns the sum of the elements.

  • IfAis a matrix, thensum(A)returns a row vector containing the sum of each column.

  • IfAis a multidimensional array, thensum(A)operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes1while the sizes of all other dimensions remain the same.

example

S = sum(A,'all')computes the sum of all elements ofA. This syntax is valid for MATLAB®versions R2018b and later.

example

S = sum(A,dim)returns the sum along dimensiondim. For example, ifAis a matrix, thensum(A,2)is a column vector containing the sum of each row.

example

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

example

S = sum(___,outtype)returns the sum with a specified data type, using any of the input arguments in the previous syntaxes.outtypecan be'default','double', or'native'.

example

S = sum(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.sum(A,'includenan')includes allNaNvalues in the calculation whilesum(A,'omitnan')ignores them.

Examples

collapse all

Create a vector and compute the sum of its elements.

A = 1:10; S = sum(A)
S = 55

Create a matrix and compute the sum of the elements in each column.

A = [1 3 2; 4 2 5; 6 1 4]
A =3×31 3 2 4 2 5 6 1 4
S = sum(A)
S =1×311 6 11

Create a matrix and compute the sum of the elements in each row.

A = [1 3 2; 4 2 5; 6 1 4]
A =3×31 3 2 4 2 5 6 1 4
S = sum(A,2)
S =3×16 11 11

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 ofA, 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 sliceAalong 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×16 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×38 8 8

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

S4 = sum(A,[1 2 3])
S4 = 24
Sall = sum(A,'all')
Sall = 24

Create a 4-by-2-by-3 array of ones and compute the sum along the third dimension.

A = ones(4,2,3); S = sum(A,3)
S =4×23 3 3 3 3 3 3 3

Create a vector of 32-bit integers and compute theint32sum of its elements by specifying the output type asnative.

A = int32(1:10); S = sum(A,'native')
S =int3255

Create a vector and compute its sum, excludingNaNvalues.

A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; S = sum(A,'omitnan')
S = 3.3250

If you do not specify'omitnan', thensum(A)returnsNaN.

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

  • IfAis a scalar, thensum(A)returnsA.

  • IfAis an empty 0-by-0 matrix, thensum(A)returns0.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|duration
Complex Number Support:Yes

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimensiondimindicates the dimension whose length reduces to 1. Thesize(S,dim)is1, while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array,A:

  • sum(A,1)operates on successive elements in the columns ofAand returns a row vector of the sums of each column.

  • sum(A,2)operates on successive elements in the rows ofAand returns a column vector of the sums of each row.

sumreturnsAwhendimis greater thanndims(A)or whensize(A,dim)is1.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

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. Thensum(A,[1 2])returns a 1-by-1-by-3 array whose elements are the sums of each page ofA.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output data type, specified as'default','double', or'native'. These options also specify the data type in which the operation is performed.

outtype Output data type
'default' double, unless the input data type issingleorduration, in which case, the output is'native'
'double' double, unless the data type isduration, in which case,'double'is not supported
'native' same data type as the input, unless the input data type ischar, in which case,'native'is not supported

Data Types:char

NaNcondition, specified as one of these values:

  • 'includenan'— IncludeNaNvalues when computing the sum, resulting inNaN.

  • 'omitnan'— Ignore allNaNvalues in the input.

Data Types:char

Extended Capabilities

Version History

Introduced before R2006a

See Also

|||