Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Construct array with accumulation

`A = accumarray(subs,val)`

`A = accumarray(subs,val,sz)`

`A = accumarray(subs,val,sz,fun)`

`A = accumarray(subs,val,sz,fun,fillval)`

`A = accumarray(subs,val,sz,fun,fillval,issparse)`

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`

assigns the sum of those elements to the output.`accumarray`

does the same by default, but optionally can apply any function to the bins.

`full`

| `histcounts`

| `sparse`

| `sum`

Was this topic helpful?