Skip to main content
v2026.11,986 entries · CC-BY 4.0

MATLAB meshgrid: Syntax, Output Size and ndgrid Conversion

How MATLAB meshgrid builds 2-D and 3-D grids, why output is numel(y)-by-numel(x), how it differs from ndgrid, and when to skip it.

Written and maintained by CASRAI Editorial Board

Published

On this page

In MATLAB, meshgrid turns two or three coordinate vectors into full coordinate matrices, so you can evaluate a function of x and y (or x, y and z) on every grid point in one vectorised expression. The one rule to remember: with [X,Y] = meshgrid(x,y), the outputs have numel(y) rows and numel(x) columns. Each row of X is a copy of x, and each column of Y is a copy of y.

Reader task: after this page you should be able to build a grid, predict its size, plot a surface over it, convert to ndgrid ordering, and decide whether you need meshgrid at all.

What meshgrid is used for in research

Researchers use meshgrid to sample a two- or three-variable model over a regular lattice: response surfaces, potential fields, parameter sweeps and contour or surface plots. It is the standard way to prepare the X, Y inputs for surf and mesh. The same idea exists in Python as NumPy’s meshgrid, covered in our NumPy meshgrid guide.

Install or access

meshgrid is a built-in MATLAB function, so no toolbox or add-on is needed to call it. MathWorks documents it at mathworks.com/help/matlab/ref/meshgrid.html. The same name exists in GNU Octave, described in the table below.

Meshgrid in MATLAB: syntax and output size

The documented call forms are:

  • [X,Y] = meshgrid(x,y)
  • [X,Y] = meshgrid(x), equivalent to meshgrid(x,x)
  • [X,Y,Z] = meshgrid(x,y,z)
  • [X,Y,Z] = meshgrid(x), equivalent to meshgrid(x,x,x), a cube
Call Output size What each output holds
meshgrid(x,y) numel(y)-by-numel(x) Each row of X copies x; each column of Y copies y
meshgrid(x,y,z) numel(y)-by-numel(x)-by-numel(z) The 3-D analogue of the 2-D rule

If an input is not a vector, MATLAB treats it as the column x(:). Inputs can be single, double or integer types; GPU and distributed arrays accept only double or single.

Step-by-step: build a grid and evaluate a function

2-D example

  1. Define the coordinate vectors: x = 1:3; y = 1:5;
  2. Build the grid: [X,Y] = meshgrid(x,y); Both outputs are 5-by-3, because numel(y) is 5 and numel(x) is 3.
  3. Evaluate element-wise (use .^, .*, ./): X.^2 + Y.^2

Surface plot

  1. x = -2:0.25:2; y = x;
  2. [X,Y] = meshgrid(x); (one argument, so y is the same as x)
  3. F = X.*exp(-X.^2-Y.^2);
  4. surf(X,Y,F)

For more on plotting the result, see our guides to 3-D plots in MATLAB and contour plots in MATLAB.

3-D example

  1. x = 0:2:6; y = 0:1:6; z = 0:3:6; (4, 7 and 3 elements)
  2. [X,Y,Z] = meshgrid(x,y,z);
  3. F = X.^2 + Y.^2 + Z.^2; size(F) returns 7 4 3, that is numel(y), numel(x), numel(z).
  4. [X,Y,Z] = meshgrid(x); with a single input gives a 4-by-4-by-4 cube.

Output and how to read it

Row index runs along y and column index runs along x, so F(i,j) is the function value at (x(j), y(i)). This is the opposite of what many people expect from matrix notation and is the most common source of transposed plots. In the 2-D example above, X has identical rows [1 2 3] and Y has identical columns [1 2 3 4 5]'.

meshgrid vs ndgrid

ndgrid builds the same lattice but with the first two dimensions swapped: in ndgrid output, the first input vector varies along rows. For [X,Y] = ndgrid(1:2:19, 2:2:12), X varies along rows and Y along columns, the opposite of meshgrid. MathWorks documents the conversion: use pagetranspose (R2020b and later) or permute(X,[2 1 3:ndims(X)]). In its example, [X,Y,Z] = meshgrid(1:4,1:3,1:2) and ndgrid(1:4,1:3,1:2) compare equal after the transpose (isequal returns 1). Unlike meshgrid, ndgrid handles any number of dimensions. Check which ordering a downstream function expects before passing a grid to it.

Do you need meshgrid? Implicit expansion

Since R2016b, MATLAB expands a row and a column automatically in element-wise operations. MathWorks shows a surface plotted without building the full matrices: surf(x,y,x.*exp(-x.^2-(y').^2)), where y' turns y into a column. Keep meshgrid when a function explicitly needs the coordinate matrices.

Troubleshooting

My plot or result looks transposed

Check the order: meshgrid output is numel(y)-by-numel(x). If a downstream function expects ndgrid ordering, transpose with pagetranspose or permute as shown above.

Wrong sizes or dimension errors in element-wise math

Confirm with size(X) and size(Y), and use element-wise operators (.*, .^) rather than matrix operators.

Passing a matrix instead of a vector

Non-vector inputs are treated as the column x(:), so the grid may be larger than you intended. Pass vectors.

Free alternatives

Tool Equivalent Documented difference
GNU Octave [xx, yy] = meshgrid (x, y); [xx, yy, zz] = meshgrid (x, y, z); single-argument forms Octave’s reference states that for x of length M and y of length N the grid is N-by-M, and that ndgrid returns the transpose and handles 1-D to N-D. Source: Octave Forge function reference, read 2026-11-08
Python (NumPy) numpy.meshgrid See our NumPy meshgrid guide for indexing options

To repeat arrays instead of building a coordinate grid, see our repmat in MATLAB guide.

FAQ

What does meshgrid do in MATLAB?

It returns coordinate matrices from coordinate vectors. For [X,Y] = meshgrid(x,y), each row of X copies x and each column of Y copies y, giving numel(y)-by-numel(x) matrices.

What does meshgrid(x) with one argument do?

It is the same as meshgrid(x,x) for two outputs and meshgrid(x,x,x) for three.

What is the difference between meshgrid and ndgrid?

The first two dimensions are swapped. meshgrid varies x along columns; ndgrid varies the first input along rows, and works in N dimensions.

Can I avoid meshgrid?

Often yes. Since R2016b, implicit expansion lets you combine a row vector and a column vector directly in element-wise expressions.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB meshgrid: Syntax, Output Size and ndgrid Conversion

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →