%% Create Sparse Matrix with Specified Size and Preallocation
% Use spalloc to initialize a 10-by-10 all-zero sparse matrix with room for up to 20 nonzero elements.

S = spalloc(10,10,20);

% Define several elements in the matrix.

S(1:3,1:3) = magic(3)

% Show the number of nonzero elements in the matrix.

n1 = nnz(S)

% Show the amount of storage allocated for nonzero matrix elements.

n2 = nzmax(S)

%
% Copyright 2020 The MathWorks, Inc.