ndgrid

Coordinate arrays for an N-D grid, in matrix index order.

Category: Language Fundamentals > Matrices and Arrays

Syntax

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

One output array per input vector.

[X1, X2, ...] = ndgrid(x)

Uses the same vector for every dimension.

Description

In the output, the first dimension runs through x1, the second through x2, and so on, exactly as array subscripts do. This is the layout expected by interpn and griddedInterpolant. meshgrid swaps the first two dimensions, which is why its output is the transpose of ndgrid for 2-D input.

Input arguments

x1, x2, ...
Coordinate vectors, one for each dimension.

Output arguments

X1, X2, ...
Arrays of size numel(x1) by numel(x2) by ..., where Xk varies along dimension k.

Example

A 2-by-3-by-2 grid

[A, B, C] = ndgrid(1:2, 1:3, 1:2);
size(A)
size(C)
B(:, :, 1)
Output
ans = 1×3

     2     3     2
ans = 1×3

     2     3     2
ans = 2×3

     1     2     3
     1     2     3

See also

meshgrid interpn linspace repmat