| MATLAB Function Reference | ![]() |
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)
A = accumarray({subs1, subs2, ...}, val,
...)
A = accumarray(subs, val) creates an array A by accumulating elements of the vector val using the subscript in subs. Each row of the m-by-n matrix subs defines an N-dimensional subscript into the output A. Each element of val has a corresponding row in subs. accumarray collects all elements of val that correspond to identical subscripts in subs, sums those values, and stores the result in the element of A that corresponds to the subscript. Elements of A that are not referred to by any row of subs contain zero.
If subs is a nonempty matrix with N>1 columns, then A is an N-dimensional array of size max(subs,[],1). If subs is empty with N>1 columns, then A is an N-dimensional empty array with size 0-by-0-by-...-by-0. subs can also be a column vector, in which case a second column of ones is implied, and A is a column vector. subs must contain positive integers.
subs can also be a cell vector with one or more elements, each element a vector of positive integers. All the vectors must have the same length. In this case, subs is treated as if the vectors formed columns of an index matrix.
val must be a numeric, logical, or character vector with the same length as the number of rows in subs. val can also be a scalar whose value is repeated for all the rows of subs.
accumarray sums values from val using the default behavior of sum.
A = accumarray(subs,val,sz) creates an array A with size sz, where sz is a vector of positive integers. If subs is nonempty with N>1 columns, then sz must have N elements, where all(sz >= max(subs,[],1)). If subs is a nonempty column vector, then sz must be [M 1], where M >= MAX(subs). Specify sz as [] for the default behavior.
A = accumarray(subs,val,sz,fun) applies function fun to each subset of elements of val. You must specify the fun input using the @ symbol (e.g., @sin). The function fun must accept a column vector and return a numeric, logical, or character scalar, or a scalar cell. Return value A has the same class as the values returned by fun. Specify fun as [] for the default behavior. fun is @sum by default.
Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. |
A = accumarray(subs,val,sz,fun,fillval) puts the scalar value fillval in elements of A that are not referred to by any row of subs. For example, if subs is empty, then A is repmat(fillval,sz). fillval and the values returned by fun must belong to the same class.
A = accumarray(subs,val,sz,fun,fillval,issparse) creates an array A that is sparse if the scalar input issparse is equal to logical 1 (i.e., true), or full if issparse is equal to logical 0 (false). A is full by default. If issparse is true, then fillval must be zero or [], and val and the output of fun must be double.
A = accumarray({subs1, subs2, ...}, val, ...) passes multiple subs vectors in a cell array. You can use any of the four optional inputs (sz, fun, fillval, or issparse) with this syntax.
Create a 5-by-1 vector, and sum values for repeated 1-dimensional subscripts:
val = 101:105;
subs = [1; 2; 4; 2; 4]
subs =
1 % Subscript 1 of result <= val(1)
2 % Subscript 2 of result <= val(2)
4 % Subscript 4 of result <= val(3)
2 % Subscript 2 of result <= val(4)
4 % Subscript 4 of result <= val(5)
A = accumarray(subs, val)
A =
101 % A(1) = val(1) = 101
206 % A(2) = val(2)+val(4) = 102+104 = 206
0 % A(3) = 0
208 % A(4) = val(3)+val(5) = 103+105 = 208Create a 2-by-3-by-2 array, and sum values for repeated three-dimensional subscripts:
val = 101:105;
subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];
A = accumarray(subs, val)
A(:,:,1) =
101 0 0
0 0 0
A(:,:,2) =
0 0 0
206 0 208Create a 2-by-3-by-2 array, and sum values natively:
val = 101:105;
subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2];
A = accumarray(subs, int8(val), [], @(x) sum(x,'native'))
A(:,:,1) =
101 0 0
0 0 0
A(:,:,2) =
0 0 0
127 0 127
class(A)
ans =
int8Pass multiple subscript arguments in a cell array.
Create a 12-element vector V:
V = 101:112;
Create three 12-element vectors, one for each dimension of the resulting array A. Note how the indices of these vectors determine which elements of V are accumulated in A:
% index 1 index 6 => V(1)+V(6) => A(1,3,1) % | | rowsubs = [1 3 3 2 3 1 2 2 3 3 1 2]; colsubs = [3 4 2 1 4 3 4 2 2 4 3 4]; pagsubs = [1 1 2 2 1 1 2 1 1 1 2 2]; % | % index 4 => V(4) => A(2,1,2) % % A(1,3,1) = V(1) + V(6) = 101 + 106 = 207 % A(2,1,2) = V(4) = 104
Call accumarray, passing the subscript vectors in a cell array:
A = accumarray({rowsubs colsubs pagsubs}, V)
A(:,:,1) =
0 0 207 0 % A(1,3,1) is 207
0 108 0 0
0 109 0 317
A(:,:,2) =
0 0 111 0
104 0 0 219 % A(2,1,2) is 104
0 103 0 0Create an array with the max function, and fill all empty elements of that array with NaN:
val = 101:105; subs = [1 1; 2 1; 2 3; 2 1; 2 3]; A = accumarray(subs, val, [2 4], @max, NaN) A = 101 NaN NaN NaN 104 NaN 105 NaN
Create a sparse matrix using the prod function:
val = 101:105; subs = [1 1; 2 1; 2 3; 2 1; 2 3]; A = accumarray(subs, val, [2 4], @prod, 0, true) A = (1,1) 101 (2,1) 10608 (2,3) 10815
Count the number of subscripts for each bin:
val = 1;
subs = [1 1; 2 1; 2 3; 2 1; 2 3];
A = accumarray(subs, val, [2 4])
A =
1 0 0 0
2 0 2 0Create a logical array that shows which bins have two or more values:
val = 101:105;
subs = [1 1; 2 1; 2 3; 2 1; 2 3];
A = accumarray(subs, val, [2 4], @(x) length(x) > 1)
A =
0 0 0 0
1 0 1 0Group values in a cell array:
val = 101:105;
subs = [1 1; 2 1; 2 3; 2 1; 2 3];
A = accumarray(subs, val, [2 4], @(x) {x})
A =
[ 101] [] [] []
[2x1 double] [] [2x1 double] []
A{2}
ans =
104
102![]() | abs | acos | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |