Meshgrid matlab что это

от admin

meshgrid

[ X , Y ] = meshgrid( x , y ) returns 2-D grid coordinates based on the coordinates contained in vectors x and y . X is a matrix where each row is a copy of x , and Y is a matrix where each column is a copy of y . The grid represented by the coordinates X and Y has length(y) rows and length(x) columns.

[ X , Y ] = meshgrid( x ) is the same as [X,Y] = meshgrid(x,x) , returning square grid coordinates with grid size length(x) -by- length(x) .

[ X , Y , Z ] = meshgrid( x , y , z ) returns 3-D grid coordinates defined by the vectors x , y , and z . The grid represented by X , Y , and Z has size length(y) -by- length(x) -by- length(z) .

[ X , Y , Z ] = meshgrid( x ) is the same as [X,Y,Z] = meshgrid(x,x,x) , returning 3-D grid coordinates with grid size length(x) -by- length(x) -by- length(x) .

Examples

2-D Grid

Create 2-D grid coordinates with x -coordinates defined by the vector x and y -coordinates defined by the vector y .

Evaluate the expression x 2 + y 2 over the 2-D grid.

Plot Surface

Create a 2-D grid with uniformly spaced x -coordinates and y -coordinates in the interval [-2,2].

Evaluate and plot the function f ( x , y ) = x e — x 2 — y 2 over the 2-D grid.

Starting in R2016b, it is not always necessary to create the grid before operating over it. For example, computing the expression x e — x 2 — y 2 implicitly expands the vectors x and y . For more information on implicit expansion, see Array vs. Matrix Operations.

Figure contains an axes object. The axes object contains an object of type surface.

3-D Grid

Create 3-D grid coordinates from x -, y -, and z -coordinates defined in the interval [0,6], and evaluate the expression x 2 + y 2 + z 2 .

Determine the size of the grid. The three coordinate vectors have different lengths, forming a rectangular box of grid points.

Use the single-input syntax to generate a uniformly spaced 3-D grid based on the coordinates defined in x . The new grid forms a cube of grid points.

Input Arguments

x — x-coordinates of points
vector

x-coordinates of points, specified as a vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

y — y-coordinates of points
vector

y-coordinates of points, specified as a vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

z — z-coordinates of points
vector

z-coordinates of points, specified as a vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

X — x-coordinates over grid
2-D or 3-D array

x-coordinates over a grid, returned as a 2-D (two inputs) or 3-D array (three inputs).

Y — y-coordinates over grid
2-D or 3-D array

y-coordinates over a grid, returned as a 2-D (two inputs) or 3-D array (three inputs).

Z — z-coordinates over grid
3-D array

z-coordinates over a grid, returned as a 3-D array.

More About

Convert Between meshgrid and ndgrid Formats

meshgrid and ndgrid create grids using different output formats. Specifically, the first two dimensions of a grid created using one of these functions are swapped when compared to the other grid format. Some MATLAB ® functions use grids in meshgrid format, while others use ndgrid format, so it is common to convert grids between the two formats.

You can convert between these grid formats using pagetranspose ( as of R2020b ) or permute to swap the first two dimensions of the grid arrays. For example, create a 3-D grid with meshgrid .

Now transpose the first two dimensions of each grid array to convert the grid to ndgrid format, and compare the results against the outputs from ndgrid .

Using pagetranspose is equivalent to permuting the first two dimensions while leaving other dimensions the same. You can also perform this operation using permute(X,[2 1 3:ndims(X)]) .

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The inputs must be floating-point double or single.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox) .

Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

The inputs must be floating-point double or single.

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox) .

The meshgrid() Function in MATLAB

This tutorial will discuss creating a grid using the meshgrid() function in Matlab.

Create a Grid Using the meshgrid() Function in MATLAB

To create 2D and 3D grids in Matlab, we can use Matlab’s built-in function meshgrid() . In Matlab, grids are used to plot data on a 3D plane. To plot a vector or matrix on a 3D plane, we have to create a 2D or 3D grid using the meshgrid() function. In 2D plots, we pass the x and y coordinates as a vector, but in 3D, we have to pass a matrix instead of a vector. We can use the meshgrid() function to convert vectors into matrices which will be used to plot the data in a 3D plane. For example, let’s convert two vectors containing x and y coordinates to matrices using the meshgrid() function. See the code below.

In the output, we can see the difference between the small x and y and capital X and Y. We cannot use the small x and y coordinates to plot data in a 3D plane, but we can use the capital X and Y coordinates to plot the data in a 3D plane. For example, let’s create a vector to plot on a 3D plane using the X and Y coordinates and the surf() function. See the code below.

The surf() function is used to plot the matrix Z on a 3D plane. The matrix Z should have the same size as the X and Y matrices. We can also create a 3D grid using three or one input vector and three output variables using the meshgrid() function. The meshgrid() function will create a 3D grid that forms a cube of grid points. If we pass only one input vector in the meshgrid() function, the function will take the other two coordinates from the indices of the first vector.

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

Meshgrid in Matlab

By Priya PedamkarPriya Pedamkar

Meshgrid in Matlab

Introduction to Meshgrid in Matlab

Meshgrid is used to create rectangular structures from the given arrays which represent the indexing in the matrix. We can also create mesh surface plots from the specified functions or arrays which have different properties to customize the plot. They can be plotted in both two dimensional and three-dimensional space. They are used in many fields like geographical and designing sectors that have huge applications. Mesh plots have similar properties as those of other plots in Matlab.

Working of Meshgrid in Matlab

Meshgrid is mainly used in Matlab and Python. There are various syntaxes that are used in Matlab to denote the respective grid.

Hadoop, Data Science, Statistics & others

Python TutorialMachine LearningAWSArtificial Intelligence

TableauR ProgrammingPowerBIDeep Learning

Syntax:

[A, B] =meshgrid (a, b)

This is used to create a two-dimensional grid with the respective coordinates mentioned in the given vector in a and b. The resultant grid will have the length of b rows and length of columns.

Читать:
Как найти b по графику

This returns the same grid as the above one and is known as a square grid which has length of rows by length of columns.

[A, B, C] =meshgrid (a, b, c)

This is used to create a three-dimensional grid with the coordinates mentioned in a, b and c. The dimension of the resultant grid will have a length of b by length of a by the length of c respectively.

[A, B, C] =meshgrid (a)

This returns the three-dimensional grid with the coordinates and the size of the grid will have length of a by length of a.

Examples of Meshgrid in Matlab

Please find the below examples which will give you a clear idea in understanding the working principle of MeshGrid:

Example #1

Code:

a = 1:4;
b = 1:3;
[A,B] = meshgrid(a,b)

Output:

meshgrid in matlab 1

Example #2

To plot the two-dimensional grid with the given interval between -1 to 1.

Code:

a = -1:0.15:1;
b = a;
[A,B] = meshgrid(a);
F = A.*exp(-A.^3-B.^2);
surf(A,B,F)

Output:

two dimensional or three-dimension array

The coordinates which are used to draw the grid are vector and the data types that can be accepted are single, double, int8, int32, int16, uint8, uint16, uint32, uint64, int64. The resultant grid or the output grid can be two dimensional or three-dimension array in their respective x, y and z coordinates. The mesh plot can be created using the mesh function in Matlab and they have different properties with respect to the mesh plot. Please find them below:

  • Mesh plots can be customized by changing the edge color of the plot which can be flat, RGB value or interp values. The default value of the edge color is [0,0,0]. If it is mentioned as flat, then there are different colors for all the edges as mentioned in the property. We can also give the RGB triplet value which denotes the intensity levels of the colors Red, Green, and Blue.
  • Depending on the intensity levels there will be different colors. We can also change the face color of the grid by specifying the various values to it. It can be flat, interp, texturemap and RGB triplet value which has the same properties as that of edge color but the default value here is flat. If the value of the property is interp then interpolated coloring is used in the face of the resultant grid.
  • We can also change the shape of the line in the resultant plot of the mesh grid. They can be a dotted line, solid line, dashed-dotted line, or dashed line. We can also change the transparency level of each face in the plot. They can be in the interval, flat, interp, texturemap,1 being the default value. If the value of the property is flat, then there is a different transparency value for each face as given in the values mentioned in the AlphaData property.
  • Similarly, if the value of the property is interp, then there will be an interpolated transparency level for each face according to the values set by the AlphaData property. If the Face color property is in the range of [0,1] then there will be equally transparency values across all the surfaces. If the value is 1 then the face is completely transparent, if the value is zero then the face is opaque and the values between 0 to 1 are considered as semitransparent.
  • We can also change the lighting of the face for all the faces with the ‘Face Lighting’ property. They can be various values like flat, gouraud and none. If the face lighting is flat, then there will be uniform lighting across all the faces. Similarly, if the value of the property is gouraud then there will be different lighting across all the faces and none for no lighting.
Example #3

Code:

a = 0:1:5;
b = 0:2:5;
[A,B] = meshgrid(a,b);
F = A.^2 + B.^3
grids=size(F)

Output:

meshgrid in matlab 3

Example #4

Code:

[A,B] = meshgrid(-4:.4:4);
C = B.*sin(A) — A.*cos(B);
F = mesh(A,B,C,’Facecolor’,’interp’)
surf(A,B,C)

Output:

Output

We can also hide the lines from the mesh plot if not required by using hidden functions in Matlab. To change the shading of the various colors used in the face we can use the shading function. To draw the three-dimensional figure with different colors we can surf function.

Conclusion

MeshGrid is one of the most important functions in Matlab which is mainly used while creating various plots in Matlab like Surface, polar, and Mesh plots. They are used in maps for various geographical locations, designing and Computational Fluid Dynamics. So, learning the use of MeshGrid in Matlab is important to work with various plots.

Recommended Articles

This has been a guide to Meshgrid in Matlab. Here we discuss the basic concept, working, and examples of Meshgrid in Matlab respectively. You may also have a look at the following articles to learn more –

How to use Meshgrid in Matlab?

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.

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.

How to use Meshgrid in Matlab?

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.

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

3D grids

How to use Meshgrid in Matlab?

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).

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).

Похожие статьи