%% Change Output Sparsity
% Create a vector of data and a matrix ind that defines groups of the data.

data = [34 22 19 85 53 77 99 6];
ind = [1 1; 400 400; 80 80; 1 1; 400 400; 400 400; 80 80; 1 1]

% The elements of ind define a 400-by-400 matrix for the output, but only reference 3 out of the 160,000 elements. When the output of accumarray results in a large array with a low density of nonzero elements, you can save memory by specifying the issparse option as true, creating a sparse matrix instead of a full one.

B = accumarray(ind,data,[],[],[],true)

% Copyright 2020 The MathWorks, Inc.