# sum

Sum of `fi`

array elements

## Description

returns the
sum along different dimensions of the `S`

= sum(`A`

)`fi`

array
`A`

.

If

`A`

is a vector,`sum(`

returns the sum of the elements.`A`

)If

`A`

is a matrix,`sum(`

treats the columns of`A`

)`A`

as vectors, returning a row vector of the sums of each column.If

`A`

is a multidimensional array,`sum(`

treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.`A`

)

## Examples

### 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])

A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11

class(A)

ans = 'embedded.fi'

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

S = 5 11 10 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11

class(S)

ans = 'embedded.fi'

MATLAB® preserves the data type of `A`

and returns a row vector `S`

of type `embedded.fi`

.

## Input Arguments

`A`

— Input `fi`

array

`fi`

object | numeric variable

`fi`

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

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `fi`

**Complex Number Support: **Yes

`dim`

— Dimension to operate along

positive 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: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `fi`

`type`

— Output class

`'double'`

| `'native'`

Output class, specified as `'double'`

or
`'native'`

. The output class defines the data type that the operation
is performed in and returned in.

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 as input array`A`

.

**Data Types: **`char`

## Output Arguments

`S`

— Sum array

scalar | vector | matrix | multidimensional array

Sum array, returned as a scalar, vector, matrix, or multidimensional array.

**Note**

The `fimath`

object is used in the calculation of the sum. If
`SumMode`

is set to `FullPrecision`

,
`KeepLSB`

, or `KeepMSB`

, then the number of integer bits
of growth for `sum(A)`

is
`ceil(log2(size(A,dim)))`

.

## Limitations

`sum`

does not support`fi`

objects of data type`Boolean`

.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Variable-sized inputs are only supported when the

`SumMode`

property of the governing`fimath`

object is set to`SpecifyPrecision`

or`KeepLSB`

.

### HDL Code Generation

Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

## Version History

**Introduced before R2006a**

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)