Skip to content

How to use Meshgrid in Matlab?

  • by
  • 3 min read

Matrix Laboratory or MATLAB is a programming language and numeric computing environment developed by MathWorks. It is a major software used by millions of engineers and scientists for multiple applications such as machine learning, deep learning, signal processing, image, video processing, signal communications, control systems, computational biology, and more.

A meshgrid creates a rectangular grid or matrix in a two-dimensional and three-dimensional space. In this article, we will understand how the meshgrid is used in MATLAB.

Also read: How to plot an equation in Matlab?


2D grids

The 2D meshgrid produces a two-dimensional grid of coordinates which are created based on the values stored in the pre-defined vectors, x and y in this case. The X grid, or matrix created, is a matrix where each row is a copy of the vector x. Similarly, the Y grid or matrix created is a matrix where each column is a copy of the vector y.

How to use Meshgrid in Matlab?

If these coordinates are to be plotted, a function F is created with the specific matrix operations and can be plotted using the surf function. A new function is created here because the surf function is a 3D plot function, and it requires three coordinates, X, Y and Z.

The surf function stands for Surface plot.

How to use Meshgrid in Matlab?

In case you wish to keep the coordinates of both the X and Y the same, the meshgrid function can be altered by specifying only the required vector in it.

The function meshgrid(x) is the same as meshgrid(x, x). Hence the coordinates remain the same while the row and columns are interchanged. In a way, it can be said that here Y is the transpose of X.

How to use Meshgrid in Matlab?

Just like above, the plot here can also be made using the surf function.

Also read: How to plot multiple lines in Matlab?


3D grids

The 3D meshgrid produces a three-dimensional grid of coordinates which are created based on the values stored in the pre-defined vectors, x, y and z in this case. The X, Y and Z grids or matrices created are of the dimensions’ length(y)-by-length(x)-by-length(z).

How to use Meshgrid in Matlab?

In case you wish to keep the coordinates of both the X, Y and Z the same, the meshgrid function can be altered by specifying only the required vector in it. The function meshgrid(x) is the same as meshgrid(x, x, x).

Also read: How to inverse matrix in Matlab?

Ishita Maheshwari

Ishita Maheshwari

A creative nerd, TT player, an avid reader, and an engineering student.

>