Home > atmlab > math > full_or_sparse.m

full_or_sparse

PURPOSE ^

FULL_OR_SPARSE Selects if full or sparse data format shall be used

SYNOPSIS ^

function X = full_or_sparse(X,density_limit)

DESCRIPTION ^

 FULL_OR_SPARSE   Selects if full or sparse data format shall be used

    The function checks the density of non-zero elements, and if this
    denisty is below the given limit, *X* is converted to sparse (if not
    already is sparse). Otherwise, X is made sure to be full.

    The default density limit is selected considering memory usage (for
    double precision). If you want to maximise the calculation speed, a
    considerably lower limit is likely better. 

 FORMAT   X = full_or_sparse(X[,density_limit])

 OUT   X               The input variable, maybe converted to full or 
                       sparse.
 IN    X               Input variable.
 OPT   density_limit   Threshold density for full/sparse. Default is 0.5.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

full_or_sparse.m

SOURCE CODE ^

0001 % FULL_OR_SPARSE   Selects if full or sparse data format shall be used
0002 %
0003 %    The function checks the density of non-zero elements, and if this
0004 %    denisty is below the given limit, *X* is converted to sparse (if not
0005 %    already is sparse). Otherwise, X is made sure to be full.
0006 %
0007 %    The default density limit is selected considering memory usage (for
0008 %    double precision). If you want to maximise the calculation speed, a
0009 %    considerably lower limit is likely better.
0010 %
0011 % FORMAT   X = full_or_sparse(X[,density_limit])
0012 %
0013 % OUT   X               The input variable, maybe converted to full or
0014 %                       sparse.
0015 % IN    X               Input variable.
0016 % OPT   density_limit   Threshold density for full/sparse. Default is 0.5.
0017 
0018 % 2014-01-04 Patrick Eriksson
0019 
0020 function X = full_or_sparse(X,density_limit)
0021 %
0022 if nargin == 1, density_limit=0.5; end
0023 
0024 density = nnz(X) / prod(size(X));
0025 
0026 if density < density_limit
0027   if ~issparse(X)
0028     X = sparse(X);
0029   end
0030 else
0031   if issparse(X)
0032     X = full(X);
0033   end  
0034 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005