函数功能
生成绘制3D图形所需的网格数据。在计算机中进行绘图操作时,往往需要一些采样点,然后根据这些采样点来绘制出整个图形。在进行3D绘图操作时,涉及到x、y、z三组数据,而x、y这两组数据可以看做是在Oxy平面内对坐标进行采样得到的坐标对(x,y)
例如,要在“3<=x<=5,6<=y<=9,z不限制区间”这个区域内绘制一个3D图形,如果只需要整数坐标为采样点的话。我们可能需要下面这样一个坐标构成的矩阵:
(3,9),(4,9),(5,9);
(3,8),(4,8),(5,8);
(3,7),(4,7),(5,7);
(3,6),(4,6),(5,6);
在matlab中我们可以这样描述这个坐标矩阵:
把各个点的x坐标独立出来,得:
3,4,5;
3,4,5;
3,4,5;
3,4,5;
再把各个点的y坐标也独立出来:
9,9,9;
8,8,8;
7,7,7;
6,6,6;
这样对应的x、y结合,便表示了上面的坐标矩阵。meshgrid就是产生这样两个矩阵,来简化我们的操作。然后根据(x,y)计算获得z,并绘制出三维图形。
在Matlab命令窗口中键入typemeshgrid可以查看该函数的源代码(由此可以理解meshgrid的算法思想),键入docmeshgrid或者helpmeshgrid可以获得帮助文档。
语法
[X,Y]=meshgrid(x,y)
解释:输出X的每一行的数值都是复制的x的值;输出Y的每一列的数值都是复制的y的值。
[X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的
[X,Y,Z]=meshgrid(x,y,z)生成三维数组,可用来计算三变量的函数和绘制三维立体图
相关函数:plot3、mesh、surf、automesh、ndgrid
源代码
function[xx,yy,zz]=meshgrid(x,y,z)
%MESHGRIDCartesiangridin2-D/3-Dspace
%[X,Y]=MESHGRID(xgv,ygv)replicatesthegridvectorsxgvandygvto
%producethecoordinatesofarectangulargrid(X,Y).Thegridvector
%xgvisreplicatednumel(ygv)timestoformthecolumnsofX.Thegrid
%vectorygvisreplicatednumel(xgv)timestoformtherowsofY.
%
%[X,Y,Z]=MESHGRID(xgv,ygv,zgv)replicatesthegridvectorsxgv,ygv,zgv
%toproducethecoordinatesofa3Drectangulargrid(X,Y,Z).Thegrid
%vectorsxgv,ygv,zgvformthecolumnsofX,rowsofY,andpagesofZ
%respectively.(X,Y,Z)areofsizenumel(ygv)-by-numel(xgv)-by(numel(zgv).
%
%[X,Y]=MESHGRID(gv)isequivalentto[X,Y]=MESHGRID(gv,gv).
%[X,Y,Z]=MESHGRID(gv)isequivalentto[X,Y,Z]=MESHGRID(gv,gv,gv).
%
%Thecoordinatearraysaretypicallyusedfortheevaluationoffunctions
%oftwoorthreevariablesandforsurfaceandvolumetricplots.
%
%MESHGRIDandNDGRIDaresimilar,thoughMESHGRIDisrestrictedto2-D
%and3-DwhileNDGRIDsupports1-DtoN-D.In2-Dand3-Dthecoordinates
%outputbyeachfunctionarethesame,thedifferenceistheshapeofthe
%outputarrays.Forgridvectorsxgv,ygvandzgvoflengthM,NandP
%respectively,NDGRID(xgv,ygv)willoutputarraysofsizeM-by-Nwhile
%MESHGRID(xgv,ygv)outputsarraysofsizeN-by-M.Similarly,
%NDGRID(xgv,ygv,zgv)willoutputarraysofsizeM-by-N-by-Pwhile
%MESHGRID(xgv,ygv,zgv)outputsarraysofsizeN-by-M-by-P.
%
%Example:Evaluatethefunctionx*exp(-x^2-y^2)
%overtherange-2
%
%[X,Y]=meshgrid(-2:.2:2,-4:.4:4);
%Z=X.*exp(-X.^2-Y.^2);
%surf(X,Y,Z)
%
%
%Classsupportforinputsxgv,ygv,zgv:
%float:double,single
%integer:uint8,int8,uint16,int16,uint32,int32,uint64,int64
%
%SeealsoSURF,SLICE,NDGRID.
%Copyright1984-2013TheMathWorks,Inc.
ifnargin==0||(nargin>1&&nargout>nargin)
error(message('MATLAB:meshgrid:NotEnoughInputs'));
end
ifnargin==2||(nargin==1&&nargout<3)%2-Darraycase
ifnargin==1
y=x;
end
ifisempty(x)||isempty(y)
xx=zeros(0,class(x));
yy=zeros(0,class(y));
else
xrow=full(x(:)).';%Makesurexisafullrowvector.
ycol=full(y(:));%Makesureyisafullcolumnvector.
xx=repmat(xrow,size(ycol));
yy=repmat(ycol,size(xrow));
end
else%3-Darraycase
ifnargin==1
y=x;
z=x;
end
ifisempty(x)||isempty(y)||isempty(z)
xx=zeros(0,class(x));
yy=zeros(0,class(y));
zz=zeros(0,class(z));
else
nx=numel(x);
ny=numel(y);
nz=numel(z);
xx=reshape(full(x),[1nx1]);%Makesurexisafullrowvector.
yy=reshape(full(y),[ny11]);%Makesureyisafullcolumnvector.
zz=reshape(full(z),[11nz]);%Makesurezisafullpagevector.
xx=repmat(xx,ny,1,nz);
yy=repmat(yy,1,nx,nz);
zz=repmat(zz,ny,nx,1);
end
end
示例一:
x=-3:1:3;y=-2:1:2;
[X,Y]=meshgrid(x,y);
这里meshgrid(x,y)的作用是分别产生以向量x为行,向量y为列的两个大小相同的矩阵,其中x的行是从-3开始到3,每间隔1记下一个数据,并把这些数据集成矩阵X;同理y的列则是从-2到2,每间隔1记下一个数据,并集成矩阵Y。即
X=
-3-2-10123
-3-2-10123
-3-2-10123
-3-2-10123
-3-2-10123
Y=
-2-2-2-2-2-2-2
-1-1-1-1-1-1-1
0000000
1111111
2222222
示例二:
functionmain
closeall;clear;clc;
M1;M2;
end
functionM1
x=rand(3,4);
y=rand(2,3);
size_of_x=size(x)
size_of_y=size(y)
[X,Y]=meshgrid(x,y);
size_of_X=size(X)
size_of_Y=size(Y)
end
functionM2
x=rand(3,4,2);
y=rand(2,3);
size_of_x=size(x)
size_of_y=size(y)
[X,Y]=meshgrid(x,y);
size_of_X=size(X)
size_of_Y=size(Y)
end
输出结果:
size_of_x=
34
size_of_y=
23
size_of_X=
612
size_of_Y=
612
size_of_x=
342
size_of_y=
23
size_of_X=
624
size_of_Y=
624
Copyright 2023 fuwu029.com赣ICP备2022008914号-4