Main Content

Construct array with accumulation

returns array `A`

= accumarray(`subs`

,`val`

)`A`

by accumulating elements of vector `val`

using the
subscripts `subs`

. If `subs`

is a column vector,
then each element defines a corresponding subscript in the output, which is also a
column vector. The `accumarray`

function collects all elements of
`val`

that have identical subscripts in `subs`

and stores their sum in the location of `A`

corresponding to that
subscript (for index `i`

,
`A(i)=sum(val(subs(:)==i))`

). Elements of `A`

whose subscripts do not appear in `subs`

are equal to 0.

For an * m*-by-

`n`

`subs`

, each row represents an
`n`

`A`

.
The ith row of `subs`

corresponds to the ith element in the vector
`val`

.

returns
an array, `A`

= accumarray(`subs`

,`val`

,`sz`

)`A`

, with size `sz`

. Specify `sz`

as
a vector of positive integers to define the size of the output, or
as `[]`

to let the subscripts in `subs`

determine
the size of the output. Use `sz`

when `subs`

does
not reference trailing rows, columns, or dimensions that you would
like to be present in the output.

The behavior of

`accumarray`

is similar to that of the`histcounts`

function. Both functions group data into bins.`histcounts`

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

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

returns the bin counts and/or bin placement. However,`accumarray`

can apply any function to the binned data.

You can mimic the behavior of

`histcounts`

using`accumarray`

with`val = 1`

.The

`sparse`

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

.`sparse`

groups data into bins using 2-D subscripts, whereas`accumarray`

groups data into bins using*n*-dimensional subscripts.For elements with identical subscripts,

`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 bins.

`full`

| `histcounts`

| `sparse`

| `sum`