Create a vector of subscripts, `subs`

.

Use `accumarray`

with ```
val =
1
```

to count the number of identical subscripts in `subs`

.

The result is a vector of bin counts. You can obtain the same
answer with `histcounts(subs,'BinMethod','integers')`

.
However, `accumarray`

also can compute bin counts
over higher dimensional grids.

Create a vector of data, `val`

, and a
vector of subscript values with the same length, `subs`

.

Use `accumarray`

to sum the values in `val`

that
have identical subscripts in `subs`

.

The result is a vector of accumulated values. Since the second
and fourth elements of `subs`

are equal to 3, `A(3)`

is
the sum of the second and fourth elements of `val`

,
that is, `A(3) = 102 + 104 = 206`

. Also, ```
A(2)
= 0
```

because `subs`

does not contain the
value `2`

. Since `subs`

is a vector,
the output, `A`

, is also a vector. The length of `A`

is `max(subs,[],1)`

.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1
2 2
3 2
1 1
2 2
4 1

The subscripts in `subs`

define a 4-by-2 matrix
for the output.

Use `accumarray`

to sum the values in `val`

that
have identical subscripts in `subs`

.

A =
205 0
0 207
0 103
106 0

The result is a 4-by-2 matrix of accumulated values.

Use the `sz`

input of `accumarray`

to
return a 4-by-4 matrix. You can specify a size with each dimension
equal to or greater than the default size, in this case 4-by-2, but
not smaller.

A =
205 0 0 0
0 207 0 0
0 103 0 0
106 0 0 0

The result is a 4-by-4 matrix of accumulated values.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1
1 1
2 2
3 2
2 2
3 2

The subscripts in `subs`

define a 3-by-2 matrix
for the output.

Use the `fun`

input of `accumarray`

to
calculate the within-group variances of data in `val`

that
have identical subscripts in `subs`

. Specify `fun`

as `@var`

.

A1 =
0.6050 0
0 3.1250
0 0.8450

The result is a 3-by-2 matrix of variance values.

Alternatively, you can specify `fun`

as
an anonymous function so long as it accepts vector inputs and returns
a scalar. A common situation where this is useful is when you want
to pass additional parameters to a function. In this case, use the `var`

function
with a normalization parameter.

A2 =
0.3025 0
0 1.5625
0 0.4225

The result is a 3-by-2 matrix of normalized variance values.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1 1
1 1 1
1 1 2
1 1 2
2 3 1
2 3 2

The subscripts in `subs`

define a 2-by-3-by-2
multidimensional array for the output.

Use `accumarray`

to sum the data values
in `val`

that have identical subscripts in `subs`

.
You can use a function handle to sum the values in their native, `int8`

,
integer class by using the `'native'`

option of the `sum`

function.

A(:,:,1) =
21 0 0
0 0 14
A(:,:,2) =
25 0 0
0 0 15

The result is a 2-by-3-by-2 multidimensional array of class `int8`

.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1
1 1
1 1
1 1
2 1
2 1
2 1
2 1
2 1
2 2

The subscripts in `subs`

define a 2-by-2 matrix
for the output.

Use `accumarray`

to group the elements
of `val`

into a cell array.

A =
[4x1 double] []
[5x1 double] [10]

The result is a 2-by-2 cell array.

Verify that the vector elements are in the same order
as they appear in `val`

.

Since the subscripts in `subs`

are sorted,
the elements of the numeric vectors in the cell array are in the same
order as they appear in `val`

.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 2
1 1
1 2
1 1
2 3

The subscripts in `subs`

define a 2-by-3 matrix
for the output, but are unsorted with respect to the linear indices
in the output, `A`

.

Group the values in `val`

into a cell
array by specifying `fun = @(x) {x}`

.

A =
[2x1 double] [2x1 double] []
[] [] [5]

The result is a 2-by-3 cell array.

Examine the vector in `A{1,2}`

.

The elements of the `A{1,2}`

vector are in
a different order than in `val`

. The first element
of the vector is 3 instead of 1. If the subscripts in `subs`

are
not sorted with respect to their linear indices, then `accumarray`

might
not always preserve the order of the data in `val`

when
it passes them to `fun`

. In the unusual case that `fun`

requires
that its input values be in the same order as they appear in `val`

,
sort the indices in `subs`

with respect to the linear
indices of the output.

In this case, use the `sortrows`

function
with two inputs and two outputs to reorder `subs`

and `val`

concurrently
with respect to the linear indices of the output.

The elements of the `A{1,2}`

vector are now
in sorted order.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1
2 2
3 3
1 1
2 2
4 4

The subscripts in `subs`

define a 4-by-4 matrix
for the output, but only reference 4 out of the 16 elements. By default,
the other 12 elements are `0`

in the output.

Use the `fillval`

input of `accumarray`

to
fill in the extra output elements with NaN values.

A =
205 NaN NaN NaN
NaN 207 NaN NaN
NaN NaN 103 NaN
NaN NaN NaN 106

The result is a 4-by-4 matrix padded with NaN values.

Create a vector of data, `val`

, and a
matrix of subscripts, `subs`

.

subs =
1 1
400 400
80 80
1 1
400 400
400 400
80 80
1 1

The subscripts in `subs`

define a 400-by-400
matrix for the output, but only reference 3 out of the 160,000 elements.
When the result of an operation with `accumarray`

leads
to a large output array with low density of nonzero elements, you
can save storage space by storing the output as a sparse matrix.

Use the `issparse`

input of `accumarray`

to
return a sparse matrix.

A =
(1,1) 125
(80,80) 118
(400,400) 152

The result is a sparse matrix. You can obtain the same answer
with `sparse(subs(:,1),subs(:,2),val)`

.