MATLAB Function Reference Previous page   Next Page
accumarray

Construct an array with accumulation

Syntax

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.

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.

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

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

Set

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

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

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

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

See Also

full, sparse, sum.


Previous page  abs acos Next page

© 1994-2005 The MathWorks, Inc.