meshgrid 的使用方法

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

附注:例题中meshgrid(-3:1:3,-2:1:2);因为-3:1:3产生的是含有7个数字的行向量;-2:1:2产生的是含有5个数字的行向量。所以该命令的结果是产生5*7的矩阵(X,Y都是5*7的矩阵;其中X是由第一个含7个元素的行向量产生,Y是由第二个行向量产生)

 

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);(三维图)

meshgrid函数



一个列子:

import numpy

all_points = numpy.random.random((1000, 1000))  # Input array

# Size of your array of points all_points:
(image_size_x, image_size_y) = all_points.shape

# Disk definition:
(center_x, center_y) = (500, 500)
radius = 10

x_grid, y_grid = numpy.meshgrid(numpy.arange(image_size_x), numpy.arange(image_size_y)) ————生成了(x, y)二维格网
                                
# Array of booleans with the disk shape
disk = ((x_grid-center_x)**2 + (y_grid-center_y)**2) <= radius**2  ————disk 是距离中心格网点(center_x, center_y)  小于等于 radius**2(x, y)

# You can now do all sorts of things with the mask "disk":

# For instance, the following array has only 317 points (about pi*radius**2):
points_in_disk = all_points[disk]

# You can also use masked arrays:
points_in_circle2 = numpy.ma.masked_array(all_points, ~disk)  ————数据掩膜处理

from matplotlib import pyplot

pyplot.imshow(points_in_circle2)  -----------matplotlib中使用imshow绘制二维图 




另一篇:

meshgrid 的使用方法:
[X,Y] = meshgrid(x,y) 将向量x和y定义的区域转换成矩阵X和Y,这两个矩阵可以用来表示mesh和surf的三维空间点以及两个变量的赋值。其中矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制。
 

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


  • 9
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值