%% Sum Values Natively
% Create a vector of data, val, and a matrix of subscripts, subs.

val = int8(10:15);
subs = [1 1 1; 1 1 1; 1 1 2; 1 1 2; 2 3 1; 2 3 2]

% The subscripts in subs define a 2-by-3-by-2 multidimensional array for the output.
%
% Use accumarray to sum the data values in val that have identical subscripts in subs. You can use a function handle to sum the values in their native, int8, integer class by using the 'native' option of the sum function.

A = accumarray(subs,val,[],@(x) sum(x,'native'))

% The result is a 2-by-3-by-2 multidimensional array of class int8.
% Copyright 2012 The MathWorks, Inc.