time | Calls | line |
---|
| | 1 | function [xx,yy,zz] = meshgrid(x,y,z)
|
| | 2 | %MESHGRID Cartesian grid in 2-D/3-D space
|
| | 3 | % [X,Y] = MESHGRID(xgv,ygv) replicates the grid vectors xgv and ygv to
|
| | 4 | % produce the coordinates of a rectangular grid (X, Y). The grid vector
|
| | 5 | % xgv is replicated numel(ygv) times to form the columns of X. The grid
|
| | 6 | % vector ygv is replicated numel(xgv) times to form the rows of Y.
|
| | 7 | %
|
| | 8 | % [X,Y,Z] = MESHGRID(xgv,ygv,zgv) replicates the grid vectors xgv, ygv, zgv
|
| | 9 | % to produce the coordinates of a 3D rectangular grid (X, Y, Z). The grid
|
| | 10 | % vectors xgv,ygv,zgv form the columns of X, rows of Y, and pages of Z
|
| | 11 | % respectively. (X,Y,Z) are of size numel(ygv)-by-numel(xgv)-by(numel(zgv).
|
| | 12 | %
|
| | 13 | % [X,Y] = MESHGRID(gv) is equivalent to [X,Y] = MESHGRID(gv,gv).
|
| | 14 | % [X,Y,Z] = MESHGRID(gv) is equivalent to [X,Y,Z] = MESHGRID(gv,gv,gv).
|
| | 15 | %
|
| | 16 | % The coordinate arrays are typically used for the evaluation of functions
|
| | 17 | % of two or three variables and for surface and volumetric plots.
|
| | 18 | %
|
| | 19 | % MESHGRID and NDGRID are similar, though MESHGRID is restricted to 2-D
|
| | 20 | % and 3-D while NDGRID supports 1-D to N-D. In 2-D and 3-D the coordinates
|
| | 21 | % output by each function are the same, the difference is the shape of the
|
| | 22 | % output arrays. For grid vectors xgv, ygv and zgv of length M, N and P
|
| | 23 | % respectively, NDGRID(xgv, ygv) will output arrays of size M-by-N while
|
| | 24 | % MESHGRID(xgv, ygv) outputs arrays of size N-by-M. Similarly,
|
| | 25 | % NDGRID(xgv, ygv, zgv) will output arrays of size M-by-N-by-P while
|
| | 26 | % MESHGRID(xgv, ygv, zgv) outputs arrays of size N-by-M-by-P.
|
| | 27 | %
|
| | 28 | % Example: Evaluate the function x*exp(-x^2-y^2)
|
| | 29 | % over the range -2 < x < 2, -4 < y < 4,
|
| | 30 | %
|
| | 31 | % [X,Y] = meshgrid(-2:.2:2, -4:.4:4);
|
| | 32 | % Z = X .* exp(-X.^2 - Y.^2);
|
| | 33 | % surf(X,Y,Z)
|
| | 34 | %
|
| | 35 | %
|
| | 36 | % Class support for inputs xgv,ygv,zgv:
|
| | 37 | % float: double, single
|
| | 38 | % integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
|
| | 39 | %
|
| | 40 | % See also SURF, SLICE, NDGRID.
|
| | 41 |
|
| | 42 | % Copyright 1984-2013 The MathWorks, Inc.
|
| | 43 |
|
< 0.001 | 2 | 44 | if nargin==0 || (nargin > 1 && nargout > nargin)
|
| | 45 | error(message('MATLAB:meshgrid:NotEnoughInputs'));
|
| | 46 | end
|
| | 47 |
|
< 0.001 | 2 | 48 | if nargin == 2 || (nargin == 1 && nargout < 3) % 2-D array case
|
< 0.001 | 2 | 49 | if nargin == 1
|
| | 50 | y = x;
|
| | 51 | end
|
< 0.001 | 2 | 52 | if isempty(x) || isempty(y)
|
| | 53 | xx = zeros(0,class(x));
|
| | 54 | yy = zeros(0,class(y));
|
< 0.001 | 2 | 55 | else
|
< 0.001 | 2 | 56 | xrow = full(x(:)).'; % Make sure x is a full row vector.
|
< 0.001 | 2 | 57 | ycol = full(y(:)); % Make sure y is a full column vector.
|
< 0.001 | 2 | 58 | xx = repmat(xrow,size(ycol));
|
< 0.001 | 2 | 59 | yy = repmat(ycol,size(xrow));
|
< 0.001 | 2 | 60 | end
|
| | 61 | else % 3-D array case
|
| | 62 | if nargin == 1
|
| | 63 | y = x;
|
| | 64 | z = x;
|
| | 65 | end
|
| | 66 | if isempty(x) || isempty(y) || isempty(z)
|
| | 67 | xx = zeros(0,class(x));
|
| | 68 | yy = zeros(0,class(y));
|
| | 69 | zz = zeros(0,class(z));
|
| | 70 | else
|
| | 71 | nx = numel(x);
|
| | 72 | ny = numel(y);
|
| | 73 | nz = numel(z);
|
| | 74 | xx = reshape(full(x),[1 nx 1]); % Make sure x is a full row vector.
|
| | 75 | yy = reshape(full(y),[ny 1 1]); % Make sure y is a full column vector.
|
| | 76 | zz = reshape(full(z),[1 1 nz]); % Make sure z is a full page vector.
|
| | 77 | xx = repmat(xx, ny, 1, nz);
|
| | 78 | yy = repmat(yy, 1, nx, nz);
|
| | 79 | zz = repmat(zz, ny, nx, 1);
|
| | 80 | end
|
< 0.001 | 2 | 81 | end
|
Other subfunctions in this file are not included in this listing.