MATLAB Language Usage of `accumarray()` Function

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

accumarray allows to aggregate items of an array in various ways, potentially applying some function to the items in the process. accumarray can be thought of as a lightweight reducer (see also: Introduction to MapReduce ).

This topic will contain common scenarios where accumarray is especially useful.

Syntax

  • accumarray(subscriptArray, valuesArray)
  • accumarray(subscriptArray, valuesArray, sizeOfOutput)
  • accumarray(subscriptArray, valuesArray, sizeOfOutput, funcHandle)
  • accumarray(subscriptArray, valuesArray, sizeOfOutput, funcHandle, fillVal)
  • accumarray(subscriptArray, valuesArray, sizeOfOutput, funcHandle, fillVal, isSparse)

Parameters

ParameterDetails
subscriptArraySubscript matrix, specified as a vector of indices, matrix of indices, or cell array of index vectors.
valuesArrayData, specified as a vector or a scalar.
sizeOfOutputSize of output array, specified as a vector of positive integers.
funcHandleFunction to be applied to each set of items during aggregation, specified as a function handle or [].
fillValFill value, for when subs does not reference each element in the output.
isSparseShould the output be a sparse array?

Remarks

  • Introduced in MATLAB v7.0.

References:

  1. "Under-appreciated accumarray", by Loren Shure, February 20, 2008.
  2. accumarray in the official MATLAB documentation.


Got any MATLAB Language Question?