1、meshgrid函数用两个坐标轴上的点在平面上画格。
用法:
[X,Y]=meshgrid(x,y)
[X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的
[X,Y,Z]=meshgrid(x,y,z)生成三维数组,可用来计算三变量的函数和绘制三维立体图
例如例题1:
x=-3:1:3;y=-2:1:2;
[X,Y]= meshgrid(x,y);
这里meshigrid(x,y)的作用是产生一个以向量x为行,向量y为列的矩阵,而x是从-3开始到3,每间隔1记下一个数据,并把这些数据集成矩阵X;同理y则是从-2到2,每间隔1记下一个数据,并集成矩阵Y。即
X=
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
Y =
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
2、 meshgrid是MATLAB中用于生成网格采样点的函数。在使用MATLAB进行3-D图形绘制方面有着广泛的应用。
生成绘制3-D图形所需的网格数据。在计算机中进行绘图操作时,往往需要一些采样点,然后根据这些采样点来绘制出整个图形。在进行3-D绘图操作时,涉及到x、y、z三组数据,而x、y这两组数据可以看做是在Oxy平面内对坐标进行采样得到的坐标对(x,y)。
[X,Y] = meshgrid(x,y)
上面的描述,我们可以知道,meshgrid返回的两个矩阵X、Y必定是行数、列数相等的,且X、Y的行数都等
于输入参数y中元素的总个数,X、Y的列数都等于输入参数x中元素总个数(这个结论可以通过查看meshgrid的源代码得到,可以通过示例程序得到验证)。
[X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的
[X,Y,Z]=meshgrid(x,y,z)生成三维数组,可用来计算三变量的函数和绘制三维立体图
相关函数: plot3、mesh、surf、automesh、ndgrid
例如:axis off;
x=-2:.2:2;
y=-1:.2:3;
[xx,yy]=meshgrid(x,y);
zz=100*(yy-xx.^2).^2+(1-xx).^2;
surfc(xx,yy,zz);(三维图)
pyplot.imshow(points_in_circle2) -----------matplotlib中使用imshow绘制二维图
另一篇:
Generate X and Y matrices for three-dimensional plots
Syntax:
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)
Description:
[X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x).
[X,Y,Z] = meshgrid(x,y,z) produces three-dimensional arrays used to evaluate functions of three variables and three-dimensional volumetric plots.
Remarks:
The meshgrid function is similar to ndgrid except that the order of the first two input and output arguments is switched. That is, the statement [X,Y,Z] = meshgrid(x,y,z)
produces the same result as [Y,X,Z] = ndgrid(y,x,z)
Because of this, meshgrid is better suited to problems in two- or three-dimensional Cartesian space, while ndgrid is better suited to multidimensional problems that aren't spatially based.
meshgrid is limited to two- or three-dimensional Cartesian space.
From:网易博客
详细解释:help meshgrid
meshgrid用于从数组a和b产生网格。生成的网格矩阵A和B大小是相同的。它也可以是更高维的。
[A,B]=Meshgrid(a,b)
生成size(b)Xsize(a)大小的矩阵A和B。它相当于a从一行重复增加到size(b)行,把b转置成一列再重复增加到size(a)列。因此命令等效于:
A=ones(size(b))*a;
B=b'*ones(size(a))
如下所示:
>> a=[1:2]
a =
1 2
>> b=[3:5]
b =
3 4 5
>> [A,B]=meshgrid(a,b)
A =
1 2
1 2
1 2
B =
3 3
4 4
5 5
>> [B,A]=mes