我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

meshgrid

Rectangular grid in 2-D and 3-D space

Syntax

[X,Y] = meshgrid(xgv,ygv)
[X,Y,Z] = meshgrid(xgv,ygv,zgv)
[X,Y] = meshgrid(gv)
[X,Y,Z] = meshgrid(gv)

Description

[X,Y] = meshgrid(xgv,ygv) replicates the grid vectors xgv and ygv to produce a full grid. This grid is represented by the output coordinate arrays X and Y. The output coordinate arrays X and Y contain copies of the grid vectors xgv and ygv respectively. The sizes of the output arrays are determined by the length of the grid vectors. For grid vectors xgv and ygv of length M and N respectively, X and Y will have N rows and M columns.

[X,Y,Z] = meshgrid(xgv,ygv,zgv) produces three-dimensional coordinate arrays. The output coordinate arrays X, Y, and Z contain copies of the grid vectors xgv, ygv, and zgv respectively. The sizes of the output arrays are determined by the length of the grid vectors. For grid vectors xgv, ygv, and zgv of length M, N, and P respectively, X, Y, and Z will have N rows, M columns, and P pages.

[X,Y] = meshgrid(gv) is the same as [X,Y] = meshgrid(gv,gv). In other words, you can reuse the same grid vector in each respective dimension. The dimensionality of the output arrays is determined by the number of output arguments.

[X,Y,Z] = meshgrid(gv) is the same as [X,Y,Z] = meshgrid(gv,gv,gv). Again, the dimensionality of the output arrays is determined by the number of output arguments.

The output coordinate arrays are typically used to evaluate functions of two or three variables. They are also frequently used to create surface and volumetric plots.

Input Arguments

xgv,ygv,zgv

Grid vectors specifying a series of grid point coordinates in the x, y and z directions, respectively.

gv

Generic grid vector specifying a series of point coordinates.

Output Arguments

X,Y,Z

Output arrays that specify the full grid.

Examples

2-D Grid From Vectors

Create a full grid from two monotonically increasing grid vectors:

[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    14

Plot 3-D Functional Surface

Use meshgrid to create a gridded (X,Y) domain.

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

Evaluate the function $z(x,y) = xe^{-x^2-y^2}$ over this domain and generate a surface plot of the results.

Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)

More About

expand all

Tips

The meshgrid function is similar to ndgrid, however meshgrid is restricted to 2-D and 3-D while ndgrid supports 1-D to N-D. The coordinates output by each function are the same, but the shape of the output arrays in the first two dimensions are different. For grid vectors x1gv, x2gv and x3gv of length M, N and P respectively, meshgrid(x1gv, x2gv) will output arrays of size N-by-M while ndgrid(x1gv, x2gv) outputs arrays of size M-by-N. Similarly, meshgrid(x1gv, x2gv, x3gv) will output arrays of size N-by-M-by-P while ndgrid(x1gv, x2gv, x3gv) outputs arrays of size M-by-N-by-P. See Grid Representation in the MATLAB® Mathematics documentation for more information.

Was this topic helpful?