meshgrid — Generate coordinate grids in MATLAB and RunMat.
meshgrid expands coordinate vectors into 2-D or 3-D coordinate arrays spanning rectangular grids. Output ordering and dimensional replication follow MATLAB semantics.
Syntax
[X,Y] = meshgrid(x)
[X,Y] = meshgrid(x, y)
[X,Y,Z] = meshgrid(x, y, z)
[X,Y] = meshgrid(x, "like", prototype)
[X,Y] = meshgrid(x, y, "like", prototype)
[X,Y,Z] = meshgrid(x, y, z, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x | NumericArray | Yes | — | X-axis vector. |
y | NumericArray | Yes | — | Y-axis vector. |
z | NumericArray | No | — | Z-axis vector. |
like_kw | StringScalar | Yes | "like" | Like keyword. |
prototype | LikePrototype | Yes | — | Prototype controlling class/device residency. |
Returns
| Name | Type | Description |
|---|---|---|
X | NumericArray | Grid coordinates along X-axis. |
Y | NumericArray | Grid coordinates along Y-axis. |
Z | NumericArray | Grid coordinates along Z-axis. |
Returned values from meshgrid depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
| — | No axis vectors are provided. | meshgrid: at least one input vector is required |
| — | More than three axis vectors are provided. | meshgrid: expected at most three input vectors |
| — | The 'like' keyword is provided without a prototype argument. | meshgrid: expected prototype after 'like' |
| — | The 'like' keyword is provided multiple times. | meshgrid: multiple 'like' specifications are not supported |
| — | The 'like' keyword is in an invalid position or not final. | meshgrid: 'like' must be the final argument |
| — | A trailing option string is not recognized. | meshgrid: unrecognised option |
| — | Axis inputs are non-numeric or non-vector shapes. | meshgrid: input argument must be numeric vector data |
| — | The 'like' prototype is unsupported. | meshgrid: prototypes must be numeric arrays |
| — | Requested outputs exceed available outputs for provided axes. | meshgrid: supports at most two outputs for 2-axis inputs and three for 3-axis inputs |
| — | A third output is requested without supplying a Z-axis vector. | meshgrid: third output requested but no Z vector was supplied |
| — | Complex axis values cannot be represented in requested real output class. | meshgrid: cannot represent complex values in a real output |
How meshgrid works
meshgrid(x)is shorthand for[X, Y] = meshgrid(x, x). It produces square 2-D grids.meshgrid(x, y)yieldsXof sizelength(y) × length(x)with rows copied fromx, andYof the same size with columns copied fromy.meshgrid(x, y, z)returns three outputs sizedlength(y) × length(x) × length(z), enabling 3-D volume visualisation.- Input vectors may be row or column vectors (or even scalars). Empty vectors propagate to empty grids of matching shape.
- Complex inputs produce complex grids where each output shares the input’s complex values.
- Supplying GPU vectors (or a
'like', gpuArray(...)prototype) keeps real and complex-interleaved outputs on the GPU when an acceleration provider is active. Without provider support, RunMat gathers inputs, materialises the grid on the host, and uploads the result transparently. 'like', prototypematches both the residency (host or GPU) and numeric class (real or complex) of the prototype. Integer prototypes are promoted to double precision, consistent with MATLAB.
Does RunMat run meshgrid on the GPU?
When vector inputs already live on the GPU, RunMat uses provider reshape and repmat hooks so real and complex-interleaved axis grids can stay resident.
If provider-side construction is unavailable, RunMat gathers the 1-D axes, materialises the grids once on the host, and uploads real or complex-interleaved outputs whenever GPU residency is requested, preserving observable semantics.
Complex GPU outputs use RunMat's interleaved real/imaginary storage metadata, so downstream complex-aware GPU operations can consume them without an immediate gather.
GPU memory and residency
You usually do not need to wrap vectors with gpuArray manually. When the active acceleration provider supports uploads, RunMat keeps real and complex-interleaved outputs on the GPU. Supplying a 'like', gpuArray(...) prototype produces GPU outputs even when all inputs are host arrays, and complex GPU prototypes request complex-interleaved GPU outputs.
Examples
Generating a square 2-D grid from one vector
x = -2:2;
[X, Y] = meshgrid(x)Expected output:
X =
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2Building a rectangular grid from two different vectors
x = [0 0.5 1.0];
y = [10 20];
[X, Y] = meshgrid(x, y)Expected output:
X =
0 0.5000 1.0000
0 0.5000 1.0000
Y =
10 10 10
20 20 20Creating a volumetric grid for 3-D plotting
u = -1:1;
v = 2:4;
w = linspace(0, 1, 5);
[U, V, W] = meshgrid(u, v, w);
size(U)Expected output:
ans =
3 3 5Matching an existing GPU prototype
gx = gpuArray(single(linspace(-pi, pi, 4)));
gy = gpuArray(single([-1 0 1]));
[Xg, Yg] = meshgrid(gx, gy)Using 'like' to copy residency from another array
proto = gpuArray.zeros(1, 1, 'double');
angles = linspace(0, 2*pi, 8);
radius = [0 1 2];
[X, Y] = meshgrid(angles, radius, 'like', proto)Complex inputs produce complex grids automatically
z = [1+1i, 2+4i];
[Zx, Zy] = meshgrid(z)Using meshgrid with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how meshgrid changes the result.
Run a small meshgrid example, explain the result, then change one input and compare the output.
FAQ
How many inputs can meshgrid accept?⌄
One, two, or three numeric vectors. Use three inputs when you need volumetric (3-D) grids.
Can I request three outputs with only one or two inputs?⌄
No. RunMat follows MATLAB and requires three input vectors when three outputs are requested.
Do row or column vectors behave differently?⌄
No. Any vector shape (row, column, or scalar) is accepted. RunMat treats the linearised data identically and replicates it along the appropriate axes.
What happens with empty vectors?⌄
Empty inputs propagate to empty outputs. For example, meshgrid([], 1:3) returns 0×3 grids for both outputs.
Can I use integer vectors?⌄
Yes. Inputs are promoted to double precision internally so the outputs represent the exact same values as MATLAB.
Does meshgrid support complex numbers?⌄
Absolutely. Any imaginary components propagate into the outputs. With an acceleration provider active, complex GPU inputs and complex gpuArray prototypes produce complex-interleaved GPU outputs.
What does 'like' do?⌄
It matches the numeric class and residency (host or GPU) of the prototype array. Supply a gpuArray prototype to keep the resulting grids on the GPU.
How can providers avoid the host fall-back?⌄
Implement the meshgrid custom hook in the acceleration provider. RunMat will automatically dispatch to it once available.
Is the output always dense?⌄
Yes. meshgrid produces dense arrays. Use ndgrid when you need permuted axes or higher-dimensional grids beyond three inputs.
What error do I get if I omit all inputs?⌄
RunMat raises the MATLAB-compatible error meshgrid: at least one input vector is required.
Related Array functions
Creation
colon · eye · false · fill · inf · linspace · logspace · magic · nan · ones · peaks · rand · randi · randn · randperm · range · true · zeros
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how meshgrid is executed, line by line, in Rust.
- View the source for meshgrid 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.