Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# 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:

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

• If A is a nonempty, nonvector matrix, then sum(A) treats the columns of A as vectors and returns a row vector whose elements are the sums of each column.

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

• If A is a multidimensional array, then sum(A) treats the values along the first array dimension whose size does not equal 1 as vectors and returns an array of row vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.

example

S = sum(A,dim) sums the elements of A along dimension dim. The dim input is a positive integer scalar.

example

S = sum(___,type) accumulates in and returns an array in the class specified by type, using any of the input arguments in the previous syntaxes. type can be 'double' or 'native'.

## Examples

expand all

### Sum of Vector Elements

Create a vector and compute the sum of the elements.

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

55```

### Sum of Elements in Each Column

Create a 3-by-3 matrix.

`A = [1 3 2; 4 2 5; 6 1 4]`
```A =

1     3     2
4     2     5
6     1     4```

Compute the sum of the elements in each column.

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

11     6    11```

### Sum of Elements in Each Row

Create a 3-by-3 matrix.

`A = [1 3 2; 4 2 5; 6 1 4]`
```A =

1     3     2
4     2     5
6     1     4```

Compute the sum of the elements in each row.

`S = sum(A,2)`
```S =

6
11
11
```

### Sum of Elements in Each Plane

Create a 4-by-2-by-3 multidimensional array of ones.

`A = ones(4,2,3)`
```A(:,:,1) =

1     1
1     1
1     1
1     1

A(:,:,2) =

1     1
1     1
1     1
1     1

A(:,:,3) =

1     1
1     1
1     1
1     1```

Compute the sum along the third (dim = 3) dimension.

`S = sum(A,3)`
```S =

3     3
3     3
3     3
3     3
```

The length of the first dimension matches size(A,1), the length of the second dimension matches size(A,2), and the length of the third dimension is 1.

### Integer Overflow Due to Output Data Type

Create a vector of signed 8-bit integers and compute the sum of the elements.

```A = int8(1:20);
S = sum(A)```
```S =

210```

The output has data type double because of default behavior.

`class(S)`
```ans =

double```

To keep the same data type in the output as in the input, specify type as 'native'.

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

127```

This disagrees with the double-precision value of 210 because the output has data type int8 and is saturated.

`class(S)`
```ans =

int8```

## Input Arguments

expand all

### A — Input arrayscalar | vector | matrix | multidimensional array

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
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, the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array, A:

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

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

sum returns A if dim is greater than ndims(A).

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

### type — Output class'double' | 'native'

Output class, specified as 'double' or 'native', defines the data type that the operation is performed in and returned in.

• If type is 'double', then sum computes and returns a double-precision array, regardless of the input data type. For example, if A is single, then sum accumulates in and returns in double. This is the default behavior for integer data types when type is not specified.

• If type is 'native', sum accumulates natively and returns an array with the same data type as the input array A. For example, if A has data type int8, then sum(A,'native') accumulates in and returns in int8. This is the default behavior for single and double data types when type is not specified.

Data Types: char

## Output Arguments

expand all

### S — Sum arrayscalar | vector | matrix | multidimensional array

Sum array, returned as a scalar, vector, matrix, or multidimensional array. The dimension of A acted on by sum has size 1 in S.

The class of S is as follows:

• If the type argument is not used and the input is not single, then the output is double.

• If the type argument is not used and the input is single, then the output is single.

• If the type argument specifies 'double', then the output is double regardless of the input data type.

• If the type argument specifies 'native', then the output is the same data type as the input.

## See Also

Was this topic helpful?