# accumarray

Accumulate vector elements

## Syntax

## Description

sums groups of data by accumulating elements of
a vector `B`

= accumarray(`ind`

,`data`

)`data`

according to the groups specified in
`ind`

. The sum is then computed over each group. The values in
`ind`

define both the group the data belongs to and the index
into the output array `B`

where each group sum is stored.

To return the group sums in order, specify `ind`

as a vector.
Then for the group with index `i`

, `accumarray`

returns its sum in `B(i)`

. For example, if ```
ind = [1 1 2
2]'
```

and `data = [1 2 3 4]'`

, then ```
B =
accumarray(ind,data)
```

returns the column vector ```
B = [3
7]'
```

.

To return the group sums in another shape, specify `ind`

as a
matrix. For an *m*-by-*n* matrix
`ind`

, each row represents the group assignment and an
*n*-dimensional index into the output `B`

. For
example, if `ind`

contains two rows of the form ```
[3
4]
```

, then the sum of the corresponding elements in
`data`

is stored in the (3,4) element of
`B`

.

Elements of `B`

whose index does not appear in
`ind`

are filled with `0`

by default.

## Examples

## Input Arguments

## Output Arguments

## More About

## Tips

The behavior of

`accumarray`

is similar to the functions`groupsummary`

and`groupcounts`

for computing summary statistics by group and counting the number of elements in a group, respectively. For more grouping functionality in MATLAB^{®}, see Preprocessing Data.The behavior of

`accumarray`

is also similar to that of the`histcounts`

function.`histcounts`

groups continuous values into a 1-D range using bin edges.`accumarray`

groups data using*n*-dimensional indices.`histcounts`

can only return bin counts and bin placement.`accumarray`

can apply any function to the data.

You can mimic the behavior of

`histcounts`

using`accumarray`

with`data = 1`

.The

`sparse`

function also has accumulation behavior similar to that of`accumarray`

.`sparse`

groups data using 2-D indices, whereas`accumarray`

groups data using*n*-dimensional indices.For elements with identical indices,

`sparse`

applies the`sum`

function (for`double`

values) or the`any`

function (for`logical`

values) and returns the scalar result in the output matrix.`accumarray`

sums by default, but can apply any function to the data.

## Extended Capabilities

## See Also

**Introduced before R2006a**