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