Usage of `accumarray()` Function

Other topics

Remarks:

  • Introduced in MATLAB v7.0.

References:

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

Finding the maximum value among elements grouped by another vector

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:

Explanation of computation process

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.

Apply Filter to Image Patches and Set Each Pixel as the Mean of the Result of Each Patch

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 [accumarray()][1]:

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]);

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?

Contributors

Topic Id: 9321

Example Ids: 28854,32670

This site is not affiliated with any of the contributors.