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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
xg | NumericArray | Yes | — | Grid vector reused for every requested output dimension. |
x1 | NumericArray | Yes | — | Grid vector for the first dimension. |
x2 | NumericArray | No | — | Grid vector for the second dimension. |
xn | NumericArray | No | — | Grid vector for the nth dimension. |
Returns
| Name | Type | Description |
|---|---|---|
X1 | NumericArray | Full grid coordinates for the first dimension. |
X2 | NumericArray | Full grid coordinates for the second dimension. |
Xn | NumericArray | Full grid coordinates for the nth dimension. |
Returned values from ndgrid depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:ndgrid:MissingAxis | No grid vector inputs are provided. | ndgrid: at least one input vector is required |
RunMat:ndgrid:InvalidAxis | An input is not numeric vector data. | ndgrid: inputs must be numeric vectors |
RunMat:ndgrid:TooManyOutputs | More outputs are requested than available input grid vectors. | ndgrid: too many output arguments for the supplied grid vectors |
RunMat:ndgrid:Internal | Output allocation, GPU gather, or GPU upload fails. | ndgrid: internal error |
How ndgrid works
X = ndgrid(x)returnsxas a column-vector grid.[X1, X2, ..., Xn] = ndgrid(xg)reuses the single vectorxgfor 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 useslength(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.
Related Array functions
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
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ndgrid is executed, line by line, in Rust.
- View the source for ndgrid in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.