Back to Core Functions

meshgrid

Create 2D coordinate matrices

Syntax

[X, Y] = meshgrid(x, y)
[X, Y] = meshgrid(v)
[X, Y] = meshgrid(x, y, 'ij')

Description

Returns coordinate matrices from coordinate vectors. For vectors x and y, meshgrid returns 2D matrices X and Y where X has x values repeated across rows and Y has y values repeated down columns (MATLAB 'xy' indexing). With a single argument v, uses v for both x and y. An optional 'ij' argument switches to NumPy-style matrix indexing where X varies along the first dimension.

Parameters

NameDescription
xX-coordinate vector
y(optional)Y-coordinate vector (defaults to x if omitted)
indexing(optional)Indexing mode: 'xy' (default, MATLAB style) or 'ij' (NumPy matrix style)

Returns

Two NDArrays [X, Y] of coordinate matrices

Examples

Try It
>> [X, Y] = meshgrid(1:3, 1:2)
Try It
>> [X, Y] = meshgrid(-2:0.5:2)
Try It
>> [X, Y] = meshgrid(1:3, 1:5, 'ij')
% X is 3x5, Y is 3x5 (NumPy style)

See Also