Accelerating the pace of engineering and science

# sum

Sum of array elements

## Description

example

S= sum (A) returns the sum along different dimensions of the fi array A.

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

If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.

If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.

example

S= sum ( A, dim) sums along the dimension dim of A.

example

S = sum (___ , type ) 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'.

• If type is 'double', then sum returns a double-precision array, regardless of the input data type.

• If type is 'native', then sum returns an array with the same class of input array A.

The fimath object is used in the calculation of the sum. If SumMode is FullPrecision, KeepLSB, or KeepMSB, then the number of integer bits of growth for sum(A) is ceil(log2(size(A,dim))).

sum does not support fi objects of data type Boolean.

## Examples

expand all

### Sum of Vector Elements

Create a fi vector, and specify fimath properties in the constructor.

`A=fi([1 2 5 8 5], 'SumMode', 'KeepLSB', 'SumWordLength', 32)`
```A =

1     2     5     8     5

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11

RoundingMethod: Nearest
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: KeepLSB
SumWordLength: 32
CastBeforeSum: true```

Compute the sum of the elements of A.

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

21

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 32
FractionLength: 11

RoundingMethod: Nearest
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: KeepLSB
SumWordLength: 32
CastBeforeSum: true```

The output S is a scalar with the specified SumWordLength of 32. The FractionLength of S is 11 because SumMode was set to KeepLSB.

### Sum of Elements in Each Column

Create a fi array, and compute the sum of the elements in each column.

`A=fi([1 2 8;3 7 0;1 2 2])`
```A =

1     2     8
3     7     0
1     2     2

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11
```
`S=sum(A)`
```S =

5    11    10

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 18
FractionLength: 11
```

MATLAB® returns a row vector with the sums of each column of A. The WordLength of S has increased by two bits because ceil(log2(size(A,1)))=2. The FractionLength remains the same because the default setting of SumMode is FullPrecision.

### Sum of Elements in Each Row

Compute the sum along the second dimension (dim=2) of 3-by-3 matrix A.

`A=fi([1 2 8;3 7 0;1 2 2])`
```A =

1     2     8
3     7     0
1     2     2

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11
```
`S=sum(A, 2)`
```S =

11
10
5

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 18
FractionLength: 11
```

MATLAB returns a column vector of the sums of the elements in each row. The WordLength of S is 18 because ceil(log2(size(A,2)))=2.

### Sum of Elements Preserving Data Type

Compute the sums of the columns of A so that the output array, S, has the same data type.

`A=fi([1 2 8;3 7 0;1 2 2]), class(A)`
```A =

1     2     8
3     7     0
1     2     2

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11

ans =

embedded.fi```
`S=sum(A, 'native'), class(S)`
```S =

5    11    10

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 18
FractionLength: 11
ans =

embedded.fi```

MATLAB preserves the data type of A and returns a row vector S of type embedded.fi.

## Input Arguments

expand all

### A — Input fi arrayfi object | numeric variable

fi input array, specified as a scalar, vector, matrix, or multidimensional array.

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

Complex Number Support: Yes

### dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. dim can also be a fi object. If no value is specified, the default is the first array dimension whose size does not equal 1.

Data Types: fi|single | double | 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.

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.