function [xx,yy,zz] = meshgrid(x,y,z)
%MATLAB Code Generation Library Function

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

eml_invariant(nargin > 0 && (nargin == 1 || nargout <= nargin), ...
    'MATLAB:meshgrid:NotEnoughInputs');
xtype = coder.internal.scalarEg(x);
nx = coder.internal.indexInt(numel(x));
if nargin == 1
    y = x;
    ytype = xtype;
    ny = nx;
else
    ytype = coder.internal.scalarEg(y);
    ny = coder.internal.indexInt(numel(y));
end
if nargin == 2 || (nargin == 1 && nargout < 3) % 2-D array case
    xx = coder.nullcopy(eml_expand(xtype,[ny,nx]));
    yy = coder.nullcopy(eml_expand(ytype,[ny,nx]));
    if nx == 0 || ny == 0
    elseif coder.isRowMajor
        for i = 1:ny
            for j = 1:nx
                xx(i,j) = x(j);
                yy(i,j) = y(i);
            end
        end
    else
        for j = 1:nx
            for i = 1:ny
                xx(i,j) = x(j);
                yy(i,j) = y(i);
            end
        end
    end
else  % 3-D array case
    if nargin == 1
        z = x;
        ztype = xtype;
        nz = nx;
    else
        ztype = coder.internal.scalarEg(z);
        nz = coder.internal.indexInt(numel(z));
    end
    xx = coder.nullcopy(eml_expand(xtype,[ny,nx,nz]));
    yy = coder.nullcopy(eml_expand(ytype,[ny,nx,nz]));
    zz = coder.nullcopy(eml_expand(ztype,[ny,nx,nz]));
    if nx == 0 || ny == 0 || nz == 0
    elseif coder.isRowMajor
        for i = 1:ny
            for j = 1:nx
                for k = 1:nz
                    xx(i,j,k) = x(j);
                    yy(i,j,k) = y(i);
                    zz(i,j,k) = z(k);
                end
            end
        end
    else
        for k = 1:nz
            for j = 1:nx
                for i = 1:ny
                    xx(i,j,k) = x(j);
                    yy(i,j,k) = y(i);
                    zz(i,j,k) = z(k);
                end
            end
        end
    end
end
