Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

ndgrid

Rectangular grid in N-D space

Syntax

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

Description

example

[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(x1) specifies a single grid vector x1 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 = 

     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 = 

     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

Evaluate and plot the function

over the gridded domain

and .

Create a grid of values for the domain.

[X1,X2] = ndgrid(-2:.2:2);

Evaluate the function over the domain.

Z = X1 .* exp(-X1.^2 - X2.^2);

Generate a mesh plot of the function.

mesh(X1,X2,Z)

In R2016b and later releases, this task does not require the use of ndgrid. Instead, you can construct the grid using implicit expansion with these commands:

x = -2:.2:2;

Z1 = x.' .* exp(-(x.').^2 - x.^2);

Create a 2-D grid and calculate some function values on the grid. Interpolate between the assigned values to refine the grid.

Create a coarse grid for in the range .

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

Calculate some function values on the grid and plot the function.

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

Interpolate between the points using a more refined grid and plot the result.

[X1,Y1] = ndgrid(-5:0.125:5);
F = interpn(X,Y,f,X1,Y1,'spline');
surf(X1,Y1,F)

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:

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.

Tips

  • The ndgrid function is similar to meshgrid. However, ndgrid supports 1-D to N-D while meshgrid is restricted to 2-D and 3-D.

    The coordinates produced by each function are the same, but the shape of the output arrays in the first two dimensions is different. For grid vectors x1, x2 and x3 of lengths M, N and P, respectively, the command ndgrid(x1,x2) outputs arrays of size M-by-N while meshgrid(x1,x2) outputs arrays of size N-by-M.

    Similarly, ndgrid(x1,x2,x3) outputs arrays of size M-by-N-by-P while meshgrid(x1,x2,x3) outputs arrays of size N-by-M-by-P.

Extended Capabilities

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

Introduced before R2006a

Was this topic helpful?