Navigation

Operators and Keywords

Function List:

C++ API

: [xx, yy] = meshgrid (x, y)
: [xx, yy, zz] = meshgrid (x, y, z)
: [xx, yy] = meshgrid (x)
: [xx, yy, zz] = meshgrid (x)

Given vectors of x and y coordinates, return matrices xx and yy corresponding to a full 2-D grid.

The rows of xx are copies of x, and the columns of yy are copies of y. If y is omitted, then it is assumed to be the same as x.

If the optional z input is given, or zz is requested, then the output will be a full 3-D grid.

meshgrid is most frequently used to produce input for a 2-D or 3-D function that will be plotted. The following example creates a surface plot of the “sombrero” function.

f = @(x,y) sin (sqrt (x.^2 + y.^2)) ./ sqrt (x.^2 + y.^2);
range = linspace (-8, 8, 41);
[X, Y] = meshgrid (range, range);
Z = f (X, Y);
surf (X, Y, Z);

Programming Note: meshgrid is restricted to 2-D or 3-D grid generation. The ndgrid function will generate 1-D through N-D grids. However, the functions are not completely equivalent. If x is a vector of length M and y is a vector of length N, then meshgrid will produce an output grid which is NxM. ndgrid will produce an output which is MxN (transpose) for the same input. Some core functions expect meshgrid input and others expect ndgrid input. Check the documentation for the function in question to determine the proper input format.

See also: ndgrid, mesh, contour, surf.

Package: octave