[X,Y] =
meshgrid(x,y) returns
2-D grid coordinates based on the coordinates contained in vectors x and y. X is
a matrix where each row is a copy of x, and Y is
a matrix where each column is a copy of y. The
grid represented by the coordinates X and Y has length(y) rows
and length(x) columns.
[X,Y,Z]
= meshgrid(x,y,z) returns
3-D grid coordinates defined by the vectors x, y,
and z. The grid represented by X, Y,
and Z has size length(y)-by-length(x)-by-length(z).
Create a 2-D grid with uniformly spaced x-coordinates and y-coordinates in the interval [-2,2].
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x);
Evaluate and plot the function over the 2-D grid.
F = X.*exp(-X.^2-Y.^2);
surf(X,Y,F)
Starting in R2016b, it is not always necessary to create the grid before operating over it. For example, computing the expression implicitly expands the vectors x and y. For more information on implicit expansion, see Array vs. Matrix Operations.
meshgrid and ndgrid create grids
using different output formats. Specifically, the first two dimensions of a grid created using
one of these functions are swapped when compared to the other grid format. Some MATLAB® functions use grids in meshgrid format, while others use
ndgrid format, so it is common to convert grids between the two
formats.
You can convert between these grid formats using pagetranspose (as of R2020b) or permute to swap the first two dimensions of the grid arrays. For example, create a 3-D grid with meshgrid.
[X,Y,Z] = meshgrid(1:4,1:3,1:2);
Now transpose the first two dimensions of each grid array to convert the grid to ndgrid format, and compare the results against the outputs from ndgrid.
Using pagetranspose is equivalent to permuting the first two dimensions while leaving other dimensions the same. You can also perform this operation using permute(X,[2 1 3:ndims(X)]).
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.