gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/+matlab/+bigdata/+internal/+util/deduceReductionDimension.m

    function dim = deduceReductionDimension(adap)
% Attempt to deduce the reduction dimension from an adaptor as the first
% non-singleton dimension. If not enough information is known or there is
% any chance that the input could be [], the result is empty.

%   Copyright 2016 The MathWorks, Inc.

dim = [];
if isempty(adap.Size)
    % If we don't know any dimensions there's nothing much we can do.
    return
end

if all(adap.Size(2:end)==1)
    % All trailing dimensions are 1, so we will reduce in first dimension
    % even if unknown.
    dim = 1;
    return;
elseif isnan(adap.TallSize.Size)
    % Trailing dims are not 1 and tall dim is unknown. We can't deduce
    % anything.
    return;
end

% If we get here then the tall size is known. Let's deal with the three
% cases: 0, 1, >1

if (adap.TallSize.Size > 1)
    % Tall size is known and safe to use. Reduce in first dimension. 
    dim = 1;
    return;
end

if isequal(adap.TallSize.Size, 0)
    % We need to avoid 0x0. If we know that NDIMS>2 or that the first small
    % dim is not 0 then it is a reduction in the tall dimension. Otherwise
    % we need to abort.
    if ~isnan(adap.NDims) && (adap.NDims>2 || ~ismember(adap.SmallSizes(1), [0 nan]))
        % Can't be 0x0, so reduce in dim 1
        dim = 1;
    end
    return
end

assert( isequal(adap.TallSize.Size, 1) )
% Tall size is 1, so find the first non-unity small dimension. If we
% don't know the small dimensions or hit a NaN then we can't continue.
if isempty(adap.SmallSizes)
    return
end
idx = find(adap.SmallSizes~=1, 1, 'first');
if isempty(idx)
    % Array must be scalar. Avoid an aggregation operation by using dim2.
    dim = 2;
elseif isnan(adap.SmallSizes(idx))
    % Hit an unknown dimension
else
    % Known no-unity dimension!
    dim = idx+1;
end

end