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.
|Subscript matrix, specified as a vector of indices, matrix of indices, or cell array of index vectors.|
|Data, specified as a vector or a scalar.|
|Size of output array, specified as a vector of positive integers.|
|Function to be applied to each set of items during aggregation, specified as a function handle or |
|Fill value, for when |
|Should the output be a sparse array?|
accumarray", by Loren Shure, February 20, 2008.
accumarrayin the official MATLAB documentation.
This is an official MATLAB example
Consider the following code:
month = [1;1;2;3;8;1;3;4;9;11;9;12;3;4;11]; temperature = [57;61;60;62;45;59;64;66;40;56;38;65;61;64;55]; maxTemp = accumarray(month,temperature,,@max);
The image below demonstrates the computation process done by
accumarray in this case:
In this example, all values that have the same
month are first collected, and then the function specified by the 4th input to
accumarray (in this case,
@max) is applied to each such set.
Many modern Image Processing algorithms use patches are their basic element to work on.
For instance one could denoise patches (See BM3D Algorithm).
Yet when building the image form the processed patches we have many results for the same pixel.
One way to deal with it is taking the average (Empirical Mean) of all values of the same pixel.
The following code shows how to break an image into patches and them reconstruct the image from patches using the average by using
numRows = 5; numCols = 5; numRowsPatch = 3; numColsPatch = 3; % The Image mI = rand([numRows, numCols]); % Decomposing into Patches - Each pixel is part of many patches (Neglecting % boundariwes, each pixel is part of (numRowsPatch * numColsPatch) patches). mY = ImageToColumnsSliding(mI, [numRowsPatch, numColsPatch]); % Here one would apply some operation which work on patches % Creating image of the index of each pixel mPxIdx = reshape(1:(numRows * numCols), [numRows, numCols]); % Creating patches of the same indices mSubsAccu = ImageToColumnsSliding(mPxIdx, [numRowsPatch, numColsPatch]); % Reconstruct the image - Option A mO = accumarray(mSubsAccu(:), mY(:)) ./ accumarray(mSubsAccu(:), 1); % Reconstruct the image - Option B mO = accumarray(mSubsAccu, mY(:), [(numRows * numCols), 1], @(x) mean(x)); % Rehsape the Vector into the Image mO = reshape(mO, [numRows, numCols]);