MESHGRID Generate Grid Mesh For Plots

Section: Array Generation and Manipulations

Usage

The meshgrid function generates arrays that can be used for the generation of surface plots. The syntax is one of
  [X,Y] = meshgrid(x)
  [X,Y] = meshgrid(x,y)
  [X,Y,Z] = meshgrid(x,y,z)

where x,y,z are vectors, and X,Y,Z are matrices. In the first case [X,Y] = meshgrid(x), the rows of X and the columns of Y contain copies of the vector x. In the second case [X,Y] = meshgrid(x,y), the rows of X contain copies of x, and the columns of Y contain copies of y. In the third case, each input is copied along the row, column or slice direction of the corresponding output variable.

Example

In the first example:
--> [X,Y] = meshgrid(-2:.4:2)
X = 

 Columns 1 to 8

   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 
   -2.0000   -1.6000   -1.2000   -0.8000   -0.4000    0.0000    0.4000    0.8000 

 Columns 9 to 11

    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 
    1.2000    1.6000    2.0000 

Y = 

 Columns 1 to 8

   -2.0000   -2.0000   -2.0000   -2.0000   -2.0000   -2.0000   -2.0000   -2.0000 
   -1.6000   -1.6000   -1.6000   -1.6000   -1.6000   -1.6000   -1.6000   -1.6000 
   -1.2000   -1.2000   -1.2000   -1.2000   -1.2000   -1.2000   -1.2000   -1.2000 
   -0.8000   -0.8000   -0.8000   -0.8000   -0.8000   -0.8000   -0.8000   -0.8000 
   -0.4000   -0.4000   -0.4000   -0.4000   -0.4000   -0.4000   -0.4000   -0.4000 
    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000 
    0.4000    0.4000    0.4000    0.4000    0.4000    0.4000    0.4000    0.4000 
    0.8000    0.8000    0.8000    0.8000    0.8000    0.8000    0.8000    0.8000 
    1.2000    1.2000    1.2000    1.2000    1.2000    1.2000    1.2000    1.2000 
    1.6000    1.6000    1.6000    1.6000    1.6000    1.6000    1.6000    1.6000 
    2.0000    2.0000    2.0000    2.0000    2.0000    2.0000    2.0000    2.0000 

 Columns 9 to 11

   -2.0000   -2.0000   -2.0000 
   -1.6000   -1.6000   -1.6000 
   -1.2000   -1.2000   -1.2000 
   -0.8000   -0.8000   -0.8000 
   -0.4000   -0.4000   -0.4000 
    0.0000    0.0000    0.0000 
    0.4000    0.4000    0.4000 
    0.8000    0.8000    0.8000 
    1.2000    1.2000    1.2000 
    1.6000    1.6000    1.6000 
    2.0000    2.0000    2.0000 

Next, we use different vectors for X and for Y:

--> [X,Y] = meshgrid([1,2,3,4],[6,7,8])
X = 
 1 2 3 4 
 1 2 3 4 
 1 2 3 4 

Y = 
 6 6 6 6 
 7 7 7 7 
 8 8 8 8