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

%   Limitations:  The 'native' flag is not supported for integer types.

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

coder.internal.allowHalfInputs;
eml_invariant(nargin >= 1,'MATLAB:minrhs');
eml_invariant(nargin <= 4,'MATLAB:maxrhs');
coder.internal.prefer_const(varargin);
eml_invariant(coder.internal.isBuiltInNumeric(x) || isa(x,'half') || ...
    ischar(x) || islogical(x), ...
    'Coder:toolbox:unsupportedClass','mean',class(x));
[zero,dim,dimsupplied,omitnan,isvdim] = process_sumprod_inputs('mean',x, ...
    varargin{:});
eml_invariant(dimsupplied || eml_is_const(size(x,dim)) || ...
    isscalar(x) || size(x,dim) ~= 1, ...
    'Coder:toolbox:autoDimIncompatibility');
eml_invariant( ...
    dimsupplied || eml_is_const(size(x)) || ~isequal(x,[]), ...
    'Coder:toolbox:UnsupportedSpecialEmpty');
eml_invariant(~isinteger(zero), ...
    'Coder:toolbox:UnsupportedNativeIntegerMean');
if eml_is_const(size(x)) && ~dimsupplied && isequal(x,[])
    y = cast(coder.internal.nan,'like',zero);
    return
end
if coder.const(isvdim)
    fx = @(x)mean(x,coder.internal.indexInt(1),varargin{2:end});
    y = coder.internal.applyToMultipleDims(fx,x,dim);
elseif islogical(zero)
    y = double(any(x,dim))/size(x,dim);
elseif isa(zero,'half')
    % Perform sum in single precision.
    identity = single(zero);
    if omitnan
        [y1,counts] = combineVectorElements(@plus,x,identity,dim,omitnan);
        y = half(y1./cast(counts,'like',identity));
    else
        y1 = combineVectorElements(@plus,x,identity,dim,false);
        y = half(y1/size(x,dim));
    end
elseif omitnan && isfloat(x)
    [y,counts] = combineVectorElements(@plus,x,zero,dim,omitnan);
    y = y./cast(counts,'like',real(y));
else
    y = combineVectorElements(@plus,x,zero,dim,false)/size(x,dim);
end
