Skip to content

meshgrid

Creating grid points for 3D surface plot

[X,Y] = meshgrid(x)

[X,Y] = meshgrid(x, y)

  • x and y should be vectors. If any of x and y is non-vector, it will be first reshaped to a vector.
  • It returns matrices X and Y, which can be used to evaluate a bivariate function, or used as grid points for surface plot (see surf()).
  • The outputs X and Y have the same sizes.
  • The outputs X and Y are empty, if one of x and y is empty.

[X,Y,Z] = meshgrid(x)

[X,Y,Z] = meshgrid(x, y, z)

  • x, y and z should be vectors. If any of x, y and z is non-vector, it will be first reshaped to a vector.
  • It returns matrices X, Y and Z, which can be used to evaluate a function of 3 variables.
  • The outputs X, Y and Z have the same sizes.
  • The outputs X, Y and Z are empty, if one of x, y, and z is empty.

Note

The vertical direction corresponds to y and the horizontal direction corresponds to x. This is different from ndgrid in which the vertical direction is the x-direction and the horizontal one is the y-direction.

Example 1: The vertical direction is the y-direction, whereas the horizontal direction is the x-direction.

[X,Y]=meshgrid(1:5)
X =
 1.000   2.000   3.000   4.000   5.000
 1.000   2.000   3.000   4.000   5.000
 1.000   2.000   3.000   4.000   5.000
 1.000   2.000   3.000   4.000   5.000
 1.000   2.000   3.000   4.000   5.000

Y =
 1.000   1.000   1.000   1.000   1.000
 2.000   2.000   2.000   2.000   2.000
 3.000   3.000   3.000   3.000   3.000
 4.000   4.000   4.000   4.000   4.000
 5.000   5.000   5.000   5.000   5.000