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 andnumel(x)columns. Every row ofXis a copy ofx; every column ofYis a copy ofy.
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