function [xx,yy,zz] = meshgrid(x,y,z)
%MESHGRID Generation of X and Y arrays for 3-D plots.
%	[X,Y] = MESHGRID(x,y) transforms the domain specified by vectors
%	x and y into arrays X and Y that can be used for the evaluation
%	of functions of two variables and 3-D surface plots.
%	The rows of the output array X are copies of the vector x and
%	the columns of the output array Y are copies of the vector y.
%
%	For example, to evaluate the function  x*exp(-x^2-y^2) over the 
%	range  -2 < x < 2,  -2 < y < 2,
%
%			[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
%			Z = X .* exp(-X.^2 - Y.^2);
%			mesh(Z)
%
%	[X,Y] = MESHGRID(x) is an abbreviation for [X,Y] = MESHGRID(x,x).
%
%	[X,Y,Z] = MESHGRID(x,y,z) produces packed 3-D arrays that can
%	be used to evaluate functions of three variables and 3-D
%	volumetric plots.
%
%	See also SURF, SLICE.

% 	J.N. Little 1-30-92, CBM 2-11-92.
%	Copyright (c) 1984-93 by The MathWorks, Inc.

if nargin == 1
	y = x;
end
if isempty(x) | isempty(y)
	xx = []; yy = []; zz = [];
end

xx = full(x(:)).'; % Make sure x is a full row vector.
yy = full(y(:));   % Make sure y is a full column vector.
nx = length(xx); ny = length(yy);
xx = xx(ones(ny, 1),:);
yy = yy(:,ones(1, nx));

if nargin == 3
	% 3-D array case
	nz = length(z);
	if nz == 0
		xx = []; yy = []; zz = []; 
		return
	end
	zz = full(z(:)).'; % make sure z is a full row vector
	zz = zz(ones(nx*ny, 1),:);
	xx = xx(:);
	xx = xx(:,ones(1,nz));
	yy = yy(:);
	yy = yy(:,ones(1,nz));
end
