function [z, ind] = max(x, y, varargin)
%MAX    Maximum elements of an array.
%   M = MAX(X)
%   Z = MAX(X,Y)
%   [M,I] = MAX(X)
%   [M,I] = MAX(X,[],DIM)
%   [M,I] = MAX(X,[],VECDIM)
%   [M,I] = MAX(X,[],'all')
%   [M,I] = MAX(X,[],...,'linear')
%   MAX(...,'omitnan')
%   MAX(...,'includenan')
%   
%   The output M has the same dimension labels, if any, as the input X.
%
%   If X and Y have dimension labels, Z has a combination of both of their
%   dimension labels. Implicit expansion is used in combining X and Y.
%   Singleton dimensions (dimensions of size 1) with dimension labels are
%   introduced into X and Y as necessary to make their dimensions match.
%   Singleton dimensions are inserted at the end of each block of
%   dimensions with the same label.
%
%   Example:
%   X has dimensions 'SCB'
%   Y has dimensions 'SSCT'
%
%   X: |S |C|B| |
%   Y: |SS|C| |T|
%   Z: |SS|C|B|T|
%   
%   Output Z has dimensions 'SSCBT'.
%
%   See also MAX, DLARRAY, DLARRAY/MIN.

%   Copyright 2019-2020 The MathWorks, Inc.

if nargin < 2 || (nargin > 2 && isequal(y, []))
    % Unary function
    [fdx, xdata, args] = helperReduction(x, varargin{:});
    
    if nargout <= 1
        xdata = max(xdata, [], args{:});
    else
        [xdata, inddata] = max(xdata, [], args{:});
    end
    
    x.FormattedData = insertData(fdx, xdata);
    if nargout > 1
        ind = x;
        ind.FormattedData = insertData(fdx, inddata);
    end
    
    x = dropTrailingODims(x);
    z = x;
elseif ischar(y) || isstring(y)
    error(message('MATLAB:max:secondInputFlag'));
else
    % Binary function

    % If this file is modified to not be a standard binary element-wise
    % broadcast function, please also remove the following template tags.
    %#define $BinaryFunction max($xdata, $ydata, varargin{:})
    %#template $BinaryBroadcastTemplate
    % Template inserted by deep.dev.generateDlarrayMethods, 2020-02-26 14:14 
    
    % Extract data to work on and create a template output object.  If either
    % of the inputs are reusable it will be used as the template output.
    [z, fdz, xdata, ydata] = extractBinaryBroadcastData(...
        matlab.lang.internal.move(x), ...
        matlab.lang.internal.move(y));
    
    zdata = max(matlab.lang.internal.move(xdata), matlab.lang.internal.move(ydata), varargin{:});
    
    z = restoreBinaryBroadcastData(z, ...
        matlab.lang.internal.move(fdz), ...
        matlab.lang.internal.move(zdata));
    
    %#template
end
end
