# sum

Sum of array elements

## Syntax

• `S = sum(A)` example
• `S = sum(A,dim)` example
• `S = sum(___,outtype)` example
• `S = sum(___,nanflag)` example

## Description

example

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

example

````S = sum(A,dim)` returns the sum along dimension `dim`. For example, if `A` is a matrix, then `sum(A,2)` is a column vector containing the sum of each row.```

example

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

example

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

## Examples

collapse all

### Sum of Vector Elements

Create a vector and compute the sum of its elements.

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

### Sum of Matrix Columns

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

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

### Sum of Matrix Rows

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

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

### Sum of 3-D Array

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 = 3 3 3 3 3 3 3 3 ```

### Integer Output Type Saturation

Create a vector of 8-bit integers and compute the `int8` sum of its elements by specifying the output type as `native`.

```A = int8(1:20); S = sum(A,'native') ```
```S = 127 ```

Compute the sum using the default `double` output type (equivalent to `S = sum(A,'default')`).

```S = sum(A) ```
```S = 210 ```

The two sums differ because the `int8` output is saturated while the `double` output reflects the expected sum of elements.

### Sum Excluding `NaN`

Create a vector and compute its sum, excluding `NaN` values.

```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'`, then `sum(A)` returns `NaN`.

## Input Arguments

collapse all

### `A` — Input arrayvector | matrix | multidimensional array

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

• If `A` is a scalar, then `sum(A)` returns `A`.

• If `A` is an empty 0-by-0 matrix, then `sum(A)` returns `0`.

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

### `dim` — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Dimension `dim` indicates the dimension whose length reduces to 1. The `size(S,dim)` is `1`, 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 of `A` and returns a row vector of the sums of each column.

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

`sum` returns `A` when `dim` is greater than `ndims(A)` or when `size(A,dim)` is `1`.

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

### `outtype` — Output data type`'default'` (default) | `'double'` | `'native'`

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 is `single` or `duration`, in which case, the output is `'native'`
`'double'``double`, unless the data type is `duration`, in which case, `'double'` is not supported
`'native'`same data type as the input, unless the input data type is `char`, in which case, `'native'` is not supported

Data Types: `char`

### `nanflag` — `NaN` condition‘`includenan`' (default) | ‘`omitnan`'

`NaN` condition, specified as one of these values:

• `'includenan'` — Include `NaN` values when computing the sum, resulting in `NaN`.

• `'omitnan'` — Ignore all `NaN` values in the input.

The `sum` function does not support the `nanflag` option for `duration` arrays.

Data Types: `char`