MATLAB Function Reference
accumarray

Construct an array with accumulation

Syntax

• ``````A = accumarray(ind, val)
``````A = accumarray(ind, val, sz)
``````A = accumarray(ind, val, sz, fun)
``````A = accumarray(ind, val, sz, fun, fillvalue)
``````

Description

`A = accumarray(ind, val)` creates an array `A` from the elements of the vector `val`, using the corresponding rows of `ind` as subscripts into `A`. `val` must have the same length as the number of rows in `ind`, unless `val` is a scalar whose value is repeated for all the rows of `ind`. If `ind` is a nonempty column vector, then `A` is a column vector of length `max(ind)`. If `ind` is a nonempty matrix with `k` columns, then `A` is a `k`-dimensional array of size `max(ind,[],1)`. If `ind` is `zeros(0,k)` with `k>1`, then `A` is the `k`-dimensional empty array of size `0`-by-`0`-by-...-by-`0`. `accumarray` accumulates by adding together elements of `val` at repeated subscripts of `A`. `accumarray` fills in `A` at unspecified subscripts with the value `0`.

 Note    `val` may be full or sparse and `A` has the same sparsity as `val`. If `val` is sparse and `ind` is a column vector, then `A` is the same as `sparse(ind,1,val)`. If `val` is sparse and `ind` is a matrix with two columns, then `A` is the same as `sparse(ind(:,1),ind(:,2),val)`.

`A = accumarray(ind, val, sz)` creates an array of size `sz`, where `sz` is a row vector of nonnegative integer values. If `ind` is a nonempty column vector, then `sz` must be `[n 1`] where `n>=max(ind)`. If `ind` is a nonempty matrix with `k` columns, then `sz` must be of length `k` with `all(sz>=max(ind,[],1))`. If `ind` is `zeros(0,k)` with `k>1`, then `sz` must be of length `k` with `all(sz>=0)`. Nonzero `sz` resizes `A` to a nonempty all-zero array.

`A = accumarray(ind, val, sz, fun)` accumulates values at repeated subscripts of `A` by applying the function `fun`, which you specify by a function handle. `fun` must accept a vector and return a scalar. For example, setting `fun=@sum` produces the default behavior of `accumarray` when you do not specify `fun.`

`A = accumarray(ind, val, sz, fun, fillvalue)` where `val` is full, fills in the values of `A` at unspecified indices with the value `fillvalue`. If `ind` is empty, but `sz` resizes `A` to nonempty, then all the values of `A` are `fillvalue`.

Examples

The following command creates a vector, accumulating at the repeated index 2.

• ```A = accumarray([1; 2; 2; 4; 5],11:15)

A =

11
25
0
14
15
```

The following commands create a 3-dimensional array, accumulating at repeated subscript `(2,3,4)`.

• ```ind = [1 1 1; 2 1 2; 2 3 4; 2 3 4];
A = accumarray(ind,11:14)
A(:,:,1) =

11     0     0
0     0     0

A(:,:,2) =

0     0     0
12     0     0

A(:,:,3) =

0     0     0
0     0     0

A(:,:,4) =

0     0     0
0     0    27
```

The following command repeats the scalar `val = pi` for all the rows in `ind`.

• ```A = accumarray(ind,pi)

A(:,:,1) =

3.1416         0         0
0         0         0

A(:,:,2) =

0         0         0
3.1416         0         0

A(:,:,3) =

0     0     0
0     0     0

A(:,:,4) =

0         0         0
0         0    6.2832
```

Set

• ```ind = [1 2; 3 2; 5 5; 5 5]
val = [10.1; 10.2; 10.3; 10.4]
```

The following command does the default summation accumulation at the repeated subscript `(5,5)`.

• ```A = accumarray(ind, val);
```

The following command increases the size of `A` beyond `max(ind,[],1)`.

• ```A = accumarray(ind, val,[6 6]);
```

The following command uses `prod` instead of `sum` as the accumulation function:

• ```A = accumarray(ind, val, [6,6], @prod);
```

The following command uses `max` as the accumulation function and fills the values at unspecified subscripts with -`Inf`.

• ```A = accumarray(ind, val, [6,6], @max, -Inf);
```

`full`, `sparse`, `sum`.