NDGRID Generate N-Dimensional Grid

Section: Array Generation and Manipulations

Usage

Generates N-dimensional grids, each of which is constant in all but one dimension. The syntax for its use is either
   [y1, y2, ..., ym] = ndgrid(x1, x2, ..., xn)

where m <= n or

   [y1, y2, ..., ym] = ndgrid(x1)

which is equivalent to the first form, with x1=x2=...=xn. Each output yi is an n-dimensional array, with values such that

ndgrid is useful for evaluating multivariate functionals over a range of arguments. It is a generalization of meshgrid, except that meshgrid transposes the dimensions corresponding to the first two arguments to better fit graphical applications.

Example

Here is a simple ndgrid example
--> [a,b] = ndgrid(1:2,3:5)
a = 
  <int32>  - size: [2 3]
 
Columns 1 to 3
 1  1  1  
 2  2  2  
b = 
  <int32>  - size: [2 3]
 
Columns 1 to 3
 3  4  5  
 3  4  5  
--> [a,b,c] = ndgrid(1:2,3:5,0:1)
a = 
  <int32>  - size: [2 3 2]
(:,:,1) = 
 
Columns 1 to 3
 1  1  1  
 2  2  2  
(:,:,2) = 
 
Columns 1 to 3
 1  1  1  
 2  2  2  
b = 
  <int32>  - size: [2 3 2]
(:,:,1) = 
 
Columns 1 to 3
 3  4  5  
 3  4  5  
(:,:,2) = 
 
Columns 1 to 3
 3  4  5  
 3  4  5  
c = 
  <int32>  - size: [2 3 2]
(:,:,1) = 
 
Columns 1 to 3
 0  0  0  
 0  0  0  
(:,:,2) = 
 
Columns 1 to 3
 1  1  1  
 1  1  1  

Here we use the second form

--> [a,b,c] = ndgrid(1:3)
a = 
  <int32>  - size: [3 3 3]
(:,:,1) = 
 
Columns 1 to 3
 1  1  1  
 2  2  2  
 3  3  3  
(:,:,2) = 
 
Columns 1 to 3
 1  1  1  
 2  2  2  
 3  3  3  
(:,:,3) = 
 
Columns 1 to 3
 1  1  1  
 2  2  2  
 3  3  3  
b = 
  <int32>  - size: [3 3 3]
(:,:,1) = 
 
Columns 1 to 3
 1  2  3  
 1  2  3  
 1  2  3  
(:,:,2) = 
 
Columns 1 to 3
 1  2  3  
 1  2  3  
 1  2  3  
(:,:,3) = 
 
Columns 1 to 3
 1  2  3  
 1  2  3  
 1  2  3  
c = 
  <int32>  - size: [3 3 3]
(:,:,1) = 
 
Columns 1 to 3
 1  1  1  
 1  1  1  
 1  1  1  
(:,:,2) = 
 
Columns 1 to 3
 2  2  2  
 2  2  2  
 2  2  2  
(:,:,3) = 
 
Columns 1 to 3
 3  3  3  
 3  3  3  
 3  3  3