Rectangular grid in N-D space
[X1,X2,X3,...,Xn] = ndgrid(x1gv,x2gv,x3gv,...,xngv)
[X1,X2,...,Xn] = ndgrid(xgv)
[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.
Grid vector specifying a series of grid point coordinates in the ith dimension.
Generic grid vector specifying a series of point coordinates.
The ith dimension of the output array Xi are copies of elements of the grid vector xigv. The output arrays specify the full grid.
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)
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 output by each function are the same, but the shape of the output arrays in the first two dimensions are different. For grid vectors x1gv, x2gv and x3gv of length M, N and P respectively, ndgrid(x1gv, x2gv) will output arrays of size M-by-N while meshgrid(x1gv, x2gv) outputs arrays of size N-by-M. Similarly, ndgrid(x1gv, x2gv, x3gv) will output arrays of size M-by-N-by-P while meshgrid(x1gv, x2gv, x3gv) outputs arrays of size N-by-M-by-P. See Grid Representation in the MATLAB® Mathematics documentation for more information.