ndgrid — Generate full coordinate arrays for an N-D rectangular grid.

ndgrid expands one or more coordinate vectors into dense N-D coordinate arrays. Each output Xi contains copies of the ith grid vector along dimension i.

Syntax

[X1, X2, ..., Xn] = ndgrid(xg)
[X1, X2, ..., Xn] = ndgrid(x1, x2, ..., xn)

Inputs

NameTypeRequiredDefaultDescription
xgNumericArrayYesGrid vector reused for every requested output dimension.
x1NumericArrayYesGrid vector for the first dimension.
x2NumericArrayNoGrid vector for the second dimension.
xnNumericArrayNoGrid vector for the nth dimension.

Returns

NameTypeDescription
X1NumericArrayFull grid coordinates for the first dimension.
X2NumericArrayFull grid coordinates for the second dimension.
XnNumericArrayFull grid coordinates for the nth dimension.

Returned values from ndgrid depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:ndgrid:MissingAxisNo grid vector inputs are provided.ndgrid: at least one input vector is required
RunMat:ndgrid:InvalidAxisAn input is not numeric vector data.ndgrid: inputs must be numeric vectors
RunMat:ndgrid:TooManyOutputsMore outputs are requested than available input grid vectors.ndgrid: too many output arguments for the supplied grid vectors
RunMat:ndgrid:InternalOutput allocation, GPU gather, or GPU upload fails.ndgrid: internal error

How ndgrid works

  • X = ndgrid(x) returns x as a column-vector grid.
  • [X1, X2, ..., Xn] = ndgrid(xg) reuses the single vector xg for every requested output dimension.
  • [X1, X2, ..., Xn] = ndgrid(x1, x2, ..., xn) returns one grid per input vector.
  • Input vectors may be row vectors, column vectors, scalars, or higher-rank arrays with at most one non-singleton dimension.
  • The output size is [length(x1), length(x2), ..., length(xn)]; the one-output form uses length(x) x 1.
  • Each output preserves the corresponding input axis class independently: complex axes produce complex outputs, logical axes produce logical outputs, and dense numeric tensors preserve their RunMat dtype metadata.
  • Requesting more outputs than supplied vectors is an error unless the single-vector syntax is used.

Examples

Create a 2-D grid

x = 1:2:9;
y = [10 20 30];
[X, Y] = ndgrid(x, y)

Expected output:

% X and Y are size 5-by-3.

Reuse one vector for multiple dimensions

v = [0 1];
[X, Y, Z] = ndgrid(v)

Expected output:

% X, Y, and Z are size 2-by-2-by-2.

Create a 3-D rectangular grid

[X, Y, Z] = ndgrid(1:4, 1:3, 1:2)

Use GPU vectors

gx = gpuArray(linspace(-1, 1, 128));
gy = gpuArray(linspace(0, 2, 64));
[Xg, Yg] = ndgrid(gx, gy)

Using ndgrid with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how ndgrid changes the result.

Run a small ndgrid example, explain the result, then change one input and compare the output.

FAQ

How is ndgrid different from meshgrid?

ndgrid uses the first input along the first dimension, the second input along the second dimension, and so on. meshgrid swaps the first two dimensions for plotting-oriented X/Y grids.

How many dimensions can ndgrid create?

As many as the requested output count in the single-vector syntax, or as many as the number of supplied vectors in the multi-vector syntax.

What happens with empty vectors?

Empty vectors produce empty dense grids with the corresponding zero extent.

Can ndgrid return gpuArray outputs?

Yes. Resident real/logical gpuArray vectors use the provider ndgrid hook when available, so outputs remain on the GPU. Unsupported providers or complex axes use the host materialization fallback; upload failures raise an error instead of silently returning host arrays.

Creation

colon · eye · false · fill · full · inf · linspace · logspace · magic · meshgrid · nan · nchoosek · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat

Indexing

find · ind2sub · sub2ind

Introspection

iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ndgrid is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.