www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/mbcCompressLogicalArray.m

    function a = mbcCompressLogicalArray(a)
%MBCCOMPRESSLOGICALARRAY Decide if a logical array should be made sparse
%
%  X = COMPRESSLOGICALARRAY(X) converts X to a sparse array if that would
%  be a more efficient storage format.

%  Copyright 2000-2005 The MathWorks, Inc. and Ford Global Technologies, Inc.


if ~isempty(a)
    % Every true element in a takes 5 bytes in sparse so when the array is more
    % than 1/5 full it is more efficient to store as full rather than sparse
    if sum(a)/numel(a) < 0.2
        a = sparse(a);
    else
        a = full(a);
    end
end