www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/PreallocateStorageForNonzerosInSparseMatrixExample.m

    %% Preallocate Storage in Sparse Matrix
% Create a sparse matrix with |10| nonzero values, but which has space
% allocated for |100| nonzero values.

% Copyright 2015 The MathWorks, Inc.

S = sparse(1:10,1:10,5,20,20,100);
N = nnz(S)

%%
%
N_alloc = nzmax(S)

%%
% The |spalloc| function is a shorthand way to create a sparse matrix with
% _no_ nonzero elements but which has space allotted for some number of
% nonzeros.