function varargout = checkMonotonicGridVectors(varargin)
%CHECKMONOTONICGRIDVECTORS checks that the input grid vectors are monotonic 
%increasing and sorts them, reordering the corresponding values in V.
%   inputs and outputs: X(1), X(2), X(3), ... X(N), V

%   Copyright 2021 The MathWorks, Inc.

assert(nargout == nargin, 'Number of outputs and inputs do not match.')
V = varargin{end};
n1 = nargin-1;
X = varargin(1:n1);

for i=1:n1
    Xi = X{i};
    [unqX, idx] = unique(Xi);
    if ~isempty(Xi) && length(Xi) > length(unqX)
        error(message('MATLAB:griddedInterpolant:NonUniqueCompVecsPtsErrId'))
    end
    if ~issorted(Xi, 'monotonic')
        error(message('MATLAB:griddedInterpolant:NonMonotonicCompVecsErrId'))
    elseif ~issorted(Xi)
        Xi = unqX;
        V = extractSlices(V, idx, i);
    end
    Xi = Xi(:);
    X{i} = Xi;
end 
varargout = [X, {V}];
end
