function [x, index, linearIndex, xPrototype, xShape] = internal_unarymax(x, needsIndexOutput, args)
% This function is undocumented and will change in a future release.

%   Copyright 2020-2023 The MathWorks, Inc.

if (nargout > 3)
    [xPrototype, xShape] = deep.internal.recording.helpers.prototypeAndShape(x);
end

index = [];
if hasLinearFlag(args)
    % args is guaranteed to contain at least one flag
    [x, linearIndex] = max(x,[],args{:});
    if needsIndexOutput
        index = linearIndex;
    end
else
    if needsIndexOutput
        if isempty(args)
            [~, index] = max(x);
        else
            [~, index] = max(x,[],args{:});
        end
    end
    [x, linearIndex] = max(x,[],args{:}, 'linear');
end
end