Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Sum of array elements

`S = sum(A)`

`S = sum(A,'all')`

`S = sum(A,dim)`

`S = sum(A,vecdim)`

`S = sum(___,outtype)`

`S = sum(___,nanflag)`

`S = sum(`

returns
the sum of the elements of A along the first array dimension whose
size does not equal 1.`A`

)

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(`

computes the sum of all elements of `A`

,`'all'`

)`A`

. This syntax is valid for
MATLAB^{®} versions R2018b and later.

`S = sum(___,`

returns
the sum with a specified data type, using any of the input arguments
in the previous syntaxes. `outtype`

)`outtype`

can be `'default'`

, `'double'`

,
or `'native'`

.