function y = median(x,varargin)
%MATLAB Code Generation Library Function

%   Copyright 2002-2023 The MathWorks, Inc.
%#codegen

narginchk(1,3);
coder.internal.assert(coder.internal.isBuiltInNumeric(x), ...
    'Coder:toolbox:unsupportedClass','median',class(x));
coder.internal.prefer_const(varargin);
DIMSUPPLIED = false;
NANFLAG = 'i';
if nargin >= 3
    NANFLAG = convertToNanFlag(varargin{2});
    coder.internal.assert(NANFLAG == 'o' || NANFLAG == 'i', ...
        'MATLAB:median:unknownFlag');
    DIMSUPPLIED = true;
elseif nargin == 2
    if coder.internal.isTextRow(varargin{1})
        if coder.internal.isConst(varargin{1})
            nf = convertToNanFlag(varargin{1});
            if nf == 'u'
                DIMSUPPLIED = true;
            else
                NANFLAG = nf;
            end
        else
            coder.internal.assert(~strncmpi(varargin{1},'all', ...
                eml_max(1,strlength(varargin{1}))), ...
                'Coder:toolbox:dimNotConst');
            NANFLAG = convertToNanFlag(varargin{1});
            coder.internal.assert(NANFLAG == 'o' || NANFLAG == 'i', ...
                'MATLAB:median:unknownFlag');
        end
    else
        DIMSUPPLIED = true;
    end
end
if DIMSUPPLIED
    DIM = varargin{1};
    coder.internal.assert(coder.internal.isConst(DIM), ...
        'Coder:toolbox:dimNotConst');
    if coder.internal.isVectorDim(DIM)
        if NANFLAG == 'i'
            fxi = @(x)median(x,coder.internal.indexInt(1),'i');
            y = coder.internal.applyToMultipleDims(fxi,x,DIM);
        else
            fxo = @(x)median(x,coder.internal.indexInt(1),'o');
            y = coder.internal.applyToMultipleDims(fxo,x,DIM);
        end
        return
    end
    coder.internal.assertValidDim(DIM);
else
    coder.internal.assert(eml_is_const(size(x)) || ~isequal(x,[]), ...
        'Coder:toolbox:median_specialEmpty');
    DIM = coder.internal.constNonSingletonDim(x);
    coder.internal.assert(eml_is_const(size(x,DIM)) || ...
        isscalar(x) || ...
        size(x,DIM) ~= 1, ...
        'Coder:toolbox:autoDimIncompatibility');
    if eml_is_const(size(x)) && isequal(x,[])
        % The output size for [] is a special case when DIM is not given.
        if isfloat(x)
            y = coder.internal.nan(class(x));
        else
            y = coder.internal.scalarEg(x);
        end
        return
    end
end
if DIM > coder.internal.ndims(x)
    y = x;
    return
end
vlen = coder.internal.indexInt(size(x,DIM));
sz = size(x);
sz(DIM) = 1;
yzero = coder.internal.scalarEg(x);
y = coder.nullcopy(eml_expand(yzero,sz));
if isempty(x) || eml_ambiguous_types
    if isfloat(x)
        y(:) = coder.internal.nan(class(x));
    else
        y(:) = 0;
    end
elseif coder.internal.isConstTrue(isscalar(y))
    [y(1),x] = vmedian(x,vlen,NANFLAG); %#ok<ASGLU>
else
    npages = coder.internal.prodsize(x,'above',DIM);
    if DIM == 1
        if coder.internal.useParforConst(mfilename,npages)
            parfor i = 1:npages
                y(i) = vmedian(x(:,i),vlen,NANFLAG);
            end
        else
            for i = 1:npages
                y(i) = vmedian(x(:,i),vlen,NANFLAG);
            end
        end
    else
        ONE = coder.internal.indexInt(1);
        y = coder.internal.applyVectorFunction( ...
            @(x)vmedian(x,vlen,NANFLAG),x,DIM,ONE,yzero);
    end
end

%-------------------------------------------------------------------------------

function nf = convertToNanFlag(arg)
coder.internal.prefer_const(arg);
coder.internal.allowEnumInputs;
matchParam = @coder.internal.matchStringParameter;
if matchParam(arg,'omitnan') || matchParam(arg,'omitmissing')
    nf = 'o';
elseif matchParam(arg,'includenan') || matchParam(arg,'includemissing')
    nf = 'i';
else
    nf = 'u';
end

%-------------------------------------------------------------------------------
