%% Specify Output Size
% Create a vector of data and a matrix of output indices ind that defines groups of data.

data = 1:6
ind = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]

% Sum the values in data for each group in ind. The indices in ind define a 4-by-2 matrix of locations for the output.

B1 = accumarray(ind,data)

% Pad the output to a 4-by-4 matrix by specifying the output size as [4 4].

B2 = accumarray(ind,data,[4 4])

% Copyright 2020 The MathWorks, Inc.