ndgrid

Construct a grid of points from coordinate vectors.

Syntax

[y1, y2, ...] = ndgrid(x1)

[y1, y2, ...] = ndgrid(x1, x2, ...)

Inputs

x1, x2, ...
Coordinate vectors from which to construct a grid.
Type: double | integer
Dimension: scalar | vector | matrix

Outputs

y1
The matrix of the x1 coordinates.
y2
The matrix of the x2 coordinates.

Examples

One input 2D example.

x1 = [1:3];
[y1 y2] = ndgrid(x1)
y1 = [Matrix] 3 x 3
1 1 1 
2 2 2 
3 3 3 
y2 = [Matrix] 3 x 3
1 2 3 
1 2 3 
1 2 3

Two input 2D example:

x1 = [1:3];
x2 = [5:8];
[y1 y2] = ndgrid(x1, x2)
y1 = [Matrix] 3 x 4
1 1 1 1 
2 2 2 2 
3 3 3 3 
y2 = [Matrix] 3 x 4
5 6 7 8 
5 6 7 8 
5 6 7 8 
5 6 7 8

Comments

The ith argument, x, is used to construct grid coordinates along the ith dimension in memory and is replicated in all other dimensions. This is often useful for vectorization, in constrast to meshgrid, which is usually more suited to plotting.

When only one input argument is present, it is used to construct grid coordinates for all output axes.