%% Specify Output Size
% Create a vector of data, val, and a matrix of subscripts, subs.

val = 101:106;
subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]

% The subscripts in subs define a 4-by-2 matrix for the output.
%
% Use accumarray to sum the values in val that have identical subscripts in subs.

A = accumarray(subs,val)

% The result is a 4-by-2 matrix of accumulated values.
%
% Use the sz input of accumarray to return a 4-by-4 matrix. You can specify a size with each dimension equal to or greater than the default size, in this case 4-by-2, but not smaller.

A = accumarray(subs,val,[4 4])

% The result is a 4-by-4 matrix of accumulated values.
% Copyright 2012 The MathWorks, Inc.