home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / ELMAT.DI$ / MESHGRID.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.5 KB  |  54 lines

  1. function [xx,yy,zz] = meshgrid(x,y,z)
  2. %MESHGRID Generation of X and Y arrays for 3-D plots.
  3. %    [X,Y] = MESHGRID(x,y) transforms the domain specified by vectors
  4. %    x and y into arrays X and Y that can be used for the evaluation
  5. %    of functions of two variables and 3-D surface plots.
  6. %    The rows of the output array X are copies of the vector x and
  7. %    the columns of the output array Y are copies of the vector y.
  8. %
  9. %    For example, to evaluate the function  x*exp(-x^2-y^2) over the 
  10. %    range  -2 < x < 2,  -2 < y < 2,
  11. %
  12. %            [X,Y] = meshgrid(-2:.2:2, -2:.2:2);
  13. %            Z = X .* exp(-X.^2 - Y.^2);
  14. %            mesh(Z)
  15. %
  16. %    [X,Y] = MESHGRID(x) is an abbreviation for [X,Y] = MESHGRID(x,x).
  17. %
  18. %    [X,Y,Z] = MESHGRID(x,y,z) produces packed 3-D arrays that can
  19. %    be used to evaluate functions of three variables and 3-D
  20. %    volumetric plots.
  21. %
  22. %    See also SURF, SLICE.
  23.  
  24. %     J.N. Little 1-30-92, CBM 2-11-92.
  25. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  26.  
  27. if nargin == 1
  28.     y = x;
  29. end
  30. if isempty(x) | isempty(y)
  31.     xx = []; yy = []; zz = [];
  32. end
  33.  
  34. xx = full(x(:)).'; % Make sure x is a full row vector.
  35. yy = full(y(:));   % Make sure y is a full column vector.
  36. nx = length(xx); ny = length(yy);
  37. xx = xx(ones(ny, 1),:);
  38. yy = yy(:,ones(1, nx));
  39.  
  40. if nargin == 3
  41.     % 3-D array case
  42.     nz = length(z);
  43.     if nz == 0
  44.         xx = []; yy = []; zz = []; 
  45.         return
  46.     end
  47.     zz = full(z(:)).'; % make sure z is a full row vector
  48.     zz = zz(ones(nx*ny, 1),:);
  49.     xx = xx(:);
  50.     xx = xx(:,ones(1,nz));
  51.     yy = yy(:);
  52.     yy = yy(:,ones(1,nz));
  53. end
  54.