Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# ndgrid

Rectangular grid in N-D space

## Syntax

[X1,X2,X3,...,Xn] = ndgrid(x1gv,x2gv,x3gv,...,xngv)
[X1,X2,...,Xn] = ndgrid(xgv)

## Description

[X1,X2,X3,...,Xn] = ndgrid(x1gv,x2gv,x3gv,...,xngv) replicates the grid vectors x1gv,x2gv,x3gv,...,xngv to produce a full grid. This grid is represented by the output coordinate arrays X1,X2,X3,...,Xn. The ith dimension of any output array Xi contains copies of the grid vector xigv.

[X1,X2,...,Xn] = ndgrid(xgv) is the same as [X1,X2,...,Xn] = ndgrid(xgv,xgv,...,xgv). In other words, you can reuse the same grid vector in each respective dimension. The dimensionality of the output arrays is determined by the number of output arguments.

The coordinate arrays [X1,X2,X3,...,Xn] are typically used to evaluate functions of several variables and to create surface and volumetric plots.

## Input Arguments

 xigv Grid vector specifying a series of grid point coordinates in the ith dimension. xgv Generic grid vector specifying a series of point coordinates.

## Output Arguments

 Xi The ith dimension of the output array Xi are copies of elements of the grid vector xigv. The output arrays specify the full grid.

## Examples

Evaluate the function over the range −2 < x1 < 2,−2 < x2 < 2:

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