meshgrid

Coordinate matrices for a 2-D or 3-D grid.

Category: Language Fundamentals > Matrices and Arrays

Syntax

[X, Y] = meshgrid(x, y)

Grid from vectors x and y.

[X, Y] = meshgrid(x)

Same as meshgrid(x, x).

[X, Y, Z] = meshgrid(x, y, z)

3-D grid.

Description

After the call, X(i,j) and Y(i,j) give the coordinates of the grid point in row i and column j. Evaluating a function over the plane then takes one vectorized expression, such as Z = X.^2 + Y.^2, ready for surf, contour or mesh.

meshgrid puts x along the columns, matching the plot axes. ndgrid instead follows matrix index order.

Input arguments

x, y, z
Vectors of grid coordinates along each axis.

Output arguments

X, Y, Z
Arrays with numel(y) rows and numel(x) columns. Every row of X is a copy of x; every column of Y is a copy of y.

Example

[X, Y] = meshgrid(1:3, 1:2)
Z = X + 10*Y
Output
X = 2×3

     1     2     3
     1     2     3
Y = 2×3

     1     1     1
     2     2     2
Z = 2×3

    11    12    13
    21    22    23

See also

ndgrid linspace surf contour repmat