Main Content

ndgrid

Rectangular grid in N-D space

Description

[X1,X2,...,Xn] = ndgrid(x1,x2,...,xn) replicates the grid vectors x1,x2,...,xn to produce an n-dimensional full grid.

example

[X1,X2,...,Xn] = ndgrid(xg) specifies a single grid vector xg to use for all dimensions. The number of output arguments you specify determines the dimensionality n of the output.

Examples

collapse all

Create a 2-D grid from the vectors [1 3 5 7 9 11 13 15 17 19] and [2 4 6 8 10 12].

[X,Y] = ndgrid(1:2:19,2:2:12)
X = 10×6

     1     1     1     1     1     1
     3     3     3     3     3     3
     5     5     5     5     5     5
     7     7     7     7     7     7
     9     9     9     9     9     9
    11    11    11    11    11    11
    13    13    13    13    13    13
    15    15    15    15    15    15
    17    17    17    17    17    17
    19    19    19    19    19    19

Y = 10×6

     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12

Create a rectangular grid and calculate function values on the grid. Interpolate between the assigned values to refine the grid.

Create a coarse grid for (x,y), where the range of x is [-6,6] and the range of y is [-3,3].

[X,Y] = ndgrid(-6:0.5:6,-3:0.5:3);

Evaluate the function at the locations defined in the grid. Then, visualize the function using a surface plot. Alternatively, since R2016b, you can use implicit expansion for this task.

f = sin(X.^2) .* cos(Y.^2);
surf(Y,X,f)

Figure contains an axes object. The axes object contains an object of type surface.

Interpolate between the points on a more refined grid (Xq,Yq). Then, visualize the interpolated values using a surface plot.

[Xq,Yq] = ndgrid(-6:0.125:6,-3:0.125:3);
F = interpn(X,Y,f,Xq,Yq,"spline");
surf(Yq,Xq,F)

Figure contains an axes object. The axes object contains an object of type surface.

Input Arguments

collapse all

Grid vectors, specified as vectors containing grid coordinates for each dimension. The grid vectors implicitly define the grid. For example, in 2-D:

Grid vectors implicitly define a full grid

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Grid vector for all dimensions, specified as a vector containing grid coordinates. ndgrid uses xg as the grid vector for each dimension.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Output Arguments

collapse all

Full grid representation, returned as separate arrays. For each output array Xi, the ith dimension contains copies of the grid vector xi.

More About

collapse all

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a