Main Content

# hdl.treesum

Sum of array elements using tree architecture

Since R2022a

## Syntax

``S = hdl.treesum(A)``
``S = hdl.treesum(A, 'all')``
``S = hdl.treesum(A, dim)``

## Description

````S = hdl.treesum(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 `hdl.treesum(A)` returns the sum of the elements.If `A` is a matrix, then `hdl.treesum(A)` returns a row vector containing the sum of each column.The function `hdl.treesum` uses a tree architecture to sum elements. The tree architecture summation yields a shorter critical path, which leads to reduced latency when generating HDL code from a MATLAB Function block. When generating HDL code, the function `hdl.treesum` reduces the amount of matching delays required to sum elements.```

example

````S = hdl.treesum(A, 'all')` computes the sum of all elements of `A`.```

example

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

example

## Examples

collapse all

Create a vector and compute the sum of its elements.

```A = 1:10; S = hdl.treesum(A)```
`S = 55`

Create a matrix and compute the sum of its elements.

`A = [1 3 2; 4 2 5; 6 1 4]`
```A = 3×3 1 3 2 4 2 5 6 1 4 ```
`S = hdl.treesum(A,'all')`
```S = 28```

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

`A = [1 3 2; 4 2 5; 6 1 4]`
```A = 3×3 1 3 2 4 2 5 6 1 4 ```
`S = hdl.treesum(A)`
```S = 1×3 11 6 11 ```

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

`A = [1 3 2; 4 2 5; 6 1 4]`
```A = 3×3 1 3 2 4 2 5 6 1 4 ```
`S = hdl.treesum(A,2)`
```S = 3×1 6 11 11 ```

## Input Arguments

collapse all

Input array, specified as a scalar, vector, or 2-D matrix.

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

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

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

## Output Arguments

collapse all

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

The data type of `S` is the same data type as that of input `A`.

## Version History

Introduced in R2022a