| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)
[X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x).
[X,Y,Z] = meshgrid(x,y,z) produces three-dimensional arrays used to evaluate functions of three variables and three-dimensional volumetric plots.
The meshgrid function is similar to ndgrid except that the order of the first two input and output arguments is switched. That is, the statement
[X,Y,Z] = meshgrid(x,y,z)
produces the same result as
[Y,X,Z] = ndgrid(y,x,z)
Because of this, meshgrid is better suited to problems in two- or three-dimensional Cartesian space, while ndgrid is better suited to multidimensional problems that aren't spatially based.
meshgrid is limited to two- or three-dimensional Cartesian space.
[X,Y] = meshgrid(1:3,10:14)
X =
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Y =
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14The following example shows how to use meshgrid to create a surface plot of a function.
[X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X .* exp(-X.^2 - Y.^2); surf(X,Y,Z)

griddata, mesh, ndgrid, slice, surf
![]() | mesh, meshc, meshz | meta.class | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |