MATLAB Function Reference
ndgrid

Generate arrays for multidimensional functions and interpolation

Syntax

• ```[X1,X2,X3,...] = ndgrid(x1,x2,x3,...)
[X1,X2,...] = ndgrid(x)
```

Description

```[X1,X2,X3,...] = ndgrid(x1,x2,x3,...) ``` transforms the domain specified by vectors `x1`,`x2`,`x3...` into arrays `X1`,`X2`,`X3...` that can be used for the evaluation of functions of multiple variables and multidimensional interpolation. The `i`th dimension of the output array `Xi` are copies of elements of the vector `xi`.

```[X1,X2,...] = ndgrid(x) ``` is the same as `[X1,X2,...] = ndgrid(x,x,...)`.

Examples

Evaluate the function

over the range .

• ```[X1,X2] = ndgrid(-2:.2:2, -2:.2:2);
Z = X1 .* exp(-X1.^2 - X2.^2);
mesh(Z)

```

Remarks

The `ndgrid` function is like `meshgrid` except that the order of the first two input arguments are switched. That is, the statement

• ````[X1,X2,X3] = ndgrid(x1,x2,x3)`
```

produces the same result as

• ````[X2,X1,X3] = meshgrid(x2,x1,x3)`
```

Because of this, `ndgrid` is better suited to multidimensional problems that aren't spatially based, while `meshgrid` is better suited to problems in two- or three-dimensional Cartesian space.

`meshgrid`, `interpn`