function y = quantile(x,p,dim)
%MATLAB Code Generation Toolbox Function

%   Copyright 1993-2021 The MathWorks, Inc.
%#codegen

coder.internal.assert(nargin >= 2,'MATLAB:minrhs');
coder.internal.assert(isvector(p) && ~isempty(p), ...
    'MATLAB:quantile:BadPOrN');
coder.internal.prefer_const(p);
if isscalar(p) && isreal(p) && p(1) == floor(p(1)) && p(1) > 1
    % pnew = (1:p(1))/(p(1)+1);
    delta = 100/(p(1)+1);
    p100 = linspace(delta,100-delta,p(1));
else
    coder.internal.assert(validatep(p),'MATLAB:quantile:BadProbs');
    p100 = 100*p;
end
if nargin < 3
    y = prctile(x,p100);
else
    coder.internal.prefer_const(dim);
    y = prctile(x,p100,dim);
end

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

function y = validatep(p)
y = isreal(p);
if y
    for k = 1:numel(p)
        if p(k) < 0 || p(k) > 1
            y = false;
            return
        end
    end
end

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