%% Change Output Sparsity
% Create a vector of data, val, and a matrix of subscripts, subs.

val = [34 22 19 85 53 77 99 6];
subs = [1 1; 400 400; 80 80; 1 1; 400 400; 400 400; 80 80; 1 1]

% The subscripts in subs define a 400-by-400 matrix for the output, but only reference 3 out of the 160,000 elements. When the result of an operation with accumarray leads to a large output array with low density of nonzero elements, you can save storage space by storing the output as a sparse matrix.
%
% Use the issparse input of accumarray to return a sparse matrix.

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

% The result is a sparse matrix. You can obtain the same answer with sparse(subs(:,1),subs(:,2),val).
% Copyright 2012 The MathWorks, Inc.