meshgrid
网格采样点的函数
meshgrid是MATLAB(一款应用软件)中用于生成网格采样点的函数。在使用MATLAB进行3D图形绘制方面有着广泛的应用。
函数简介
函数功能
生成绘制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命令窗口中键入type meshgrid可以查看该函数的源代码(由此可以理解meshgrid的算法思想), 键入doc meshgrid或者help meshgrid可以获得帮助文档。
语法
[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)
%MESHGRID Cartesian grid in 2-D/3-D space
% [X,Y] = MESHGRID(xgv,ygv) replicates the grid vectors xgv and ygv to
% produce the coordinates of a rectangular grid (X, Y). The grid vector
% xgv is replicated numel(ygv) times to form the columns of X. The grid
% vector ygv is replicated numel(xgv) times to form the rows of Y.
%
% [X,Y,Z] = MESHGRID(xgv,ygv,zgv) replicates the grid vectors xgv, ygv, zgv
% to produce the coordinates of a 3D rectangular grid (X, Y, Z). The grid
% vectors xgv,ygv,zgv form the columns of X, rows of Y, and pages of Z
% respectively. (X,Y,Z) are of size numel(ygv)-by-numel(xgv)-by(numel(zgv).
%
% [X,Y] = MESHGRID(gv) is equivalent to [X,Y] = MESHGRID(gv,gv).
% [X,Y,Z] = MESHGRID(gv) is equivalent to [X,Y,Z] = MESHGRID(gv,gv,gv).
%
% The coordinate arrays are typically used for the evaluation of functions
% of two or three variables and for surface and volumetric plots.
%
% MESHGRID and NDGRID are similar, though MESHGRID is restricted to 2-D
% and 3-D while NDGRID supports 1-D to N-D. In 2-D and 3-D the coordinates
% output by each function are the same, the difference is the shape of the
% output arrays. For grid vectors xgv, ygv and zgv of length M, N and P
% respectively, NDGRID(xgv, ygv) will output arrays of size M-by-N while
% MESHGRID(xgv, ygv) outputs arrays of size N-by-M. Similarly,
% NDGRID(xgv, ygv, zgv) will output arrays of size M-by-N-by-P while
% MESHGRID(xgv, ygv, zgv) outputs arrays of size N-by-M-by-P.
%
% Example: Evaluate the function x*exp(-x^2-y^2)
% over the range -2 < x < 2, -4 < y < 4,
%
% [X,Y] = meshgrid(-2:.2:2, -4:.4:4);
% Z = X .* exp(-X.^2 - Y.^2);
% surf(X,Y,Z)
%
%
% Class support for inputs xgv,ygv,zgv:
% float: double, single
% integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
%
% See also SURF, SLICE, NDGRID.
% Copyright 1984-2013 The MathWorks, Inc.
if nargin==0 || (nargin > 1 && nargout > nargin)
error(message('MATLAB:meshgrid:NotEnoughInputs'));
end
if nargin == 2 || (nargin == 1 && nargout < 3) % 2-D array case
if nargin == 1
y = x;
end
if isempty(x) || isempty(y)
xx = zeros(0,class(x));
yy = zeros(0,class(y));
else
xrow = full(x(:)).'; % Make sure x is a full row vector.
ycol = full(y(:)); % Make sure y is a full column vector.
xx = repmat(xrow,size(ycol));
yy = repmat(ycol,size(xrow));
end
else % 3-D array case
if nargin == 1
y = x;
z = x;
end
if isempty(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),[1 nx 1]); % Make sure x is a full row vector.
yy = reshape(full(y),[ny 1 1]); % Make sure y is a full column vector.
zz = reshape(full(z),[1 1 nz]); % Make sure z is a full page vector.
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 -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
示例二:
function main
close all; clear; clc;
M1; M2;
end
function M1
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
function M2
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 =
3 4
size_of_y =
2 3
size_of_X =
6 12
size_of_Y =
6 12
size_of_x =
3 4 2
size_of_y =
2 3
size_of_X =
6 24
size_of_Y =
6 24
参考资料
最新修订时间:2024-11-05 19:37
目录
概述
函数简介
参考资料