function mx = getMaxIndex(idx)
% Get the highest index value from an index argument. Input can be a vector
% of linear indices, a boolean vector, or a ColonDescriptor. Returns 0 if
% the index vector is empty.

%   Copyright 2017-2019 The MathWorks, Inc.

if parallel.internal.indexing.isColonDescriptor(idx)
    if idx.Stride<=0
        mx = idx.Start;
    else
        lastElement = prod(size(idx)); %#ok<PSIZE> Seems to be only way to get length of ColonDescriptor
        if lastElement>0
            mx = idx(lastElement);
        else
            mx = 0;
        end
    end
elseif isempty(idx)
    mx = 0;
elseif islogical(idx)
    mx = find(idx,1,'last');
else
    mx = max(idx);
end

end
