HOWTO · MATLAB

The meshgrid() Function in MATLAB

Learn how meshgrid() in MATLAB maps coordinate vectors into matrices, evaluate a function across a 2-D grid, and visualize the result with surf().

On this page

meshgrid() in MATLAB turns coordinate vectors into coordinate matrices, so you can evaluate a function at every point in a 2-D grid and plot the resulting values. The example below uses different numbers of x and y coordinates to make the matrix orientation clear.

Understand how meshgrid() maps coordinates

Call meshgrid(x, y) with lowercase input vectors. It returns uppercase matrices X and Y: each row of X repeats the x coordinates, while each column of Y repeats a y coordinate. The rows correspond to values from y, and the columns correspond to values from x.

For example, x = 1:4 has four values and y = 1:6 has six. The resulting X and Y each have six rows and four columns. This orientation is useful to remember when checking a grid or matching it to a matrix of function values.

Create a 2-D coordinate grid

This self-contained Octave-compatible example creates a non-square grid, evaluates a function at every coordinate pair, and draws the resulting surface. It prints a compact view of the coordinate pattern and the matrix size.

warning('off', 'Octave:gnuplot-graphics');
set(0, 'defaultfigurevisible', 'off');
x = 1:4;
y = 1:6;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;

fprintf('X(1,:) =');
fprintf(' %g', X(1,:));
fprintf('\nY(:,1) =');
fprintf(' %g', Y(:,1));
fprintf('\nsize(Z) = %d %d\n', size(Z));
surf(X, Y, Z);

The first printed row shows that X repeats 1 2 3 4 across each row. The first column of Y shows the y coordinates from 1 through 6, with each value repeated across its row. Thus each pair X(i,j) and Y(i,j) gives one coordinate pair, and Z(i,j) stores the function value at that point. The .^ operator squares each matrix element; ordinary matrix power would not express this element-by-element calculation.

X(1,:) = 1 2 3 4
Y(:,1) = 1 2 3 4 5 6
size(Z) = 6 4

Evaluate and plot a function over the grid

The example computes Z = X.^2 + Y.^2 from the coordinate matrices, then passes X, Y, and Z to surf(). For this workflow, Z must have the same number of rows and columns as X and Y, so every plotted height corresponds to a coordinate pair. If you change either input vector, meshgrid() changes the grid dimensions and the element-wise expression computes matching values.

For a top-down view of function levels, MATLAB’s contour(X, Y, Z) can use the same matrices instead of surf(). It visualizes equal-value curves; it is an alternative plotting call, not an additional executed example here.

Create a 3-D coordinate grid

A three-dimensional coordinate grid is useful when a function depends on three spatial coordinates, such as a quantity evaluated throughout a volume. This article covers the two-vector meshgrid(x, y) workflow and does not specify three-dimensional call syntax or output dimensions. Consult the documentation for the MATLAB release you use before adapting the approach to a third coordinate.