基本介紹
- 外文名:meshgrid
- 軟體:MATLAB
- 實質:格線採樣點的函式
- 套用:3D圖形繪製
函式簡介,函式功能,語法,原始碼,程式示例,
函式簡介
函式功能
生成繪製3D圖形所需的格線數據。在計算機中進行繪圖操作時, 往往需要一些採樣點,然後根據這些採樣點來繪製出整個圖形。在進行3D繪圖操作時,涉及到x、y、z三組數據,而x、y這兩組數據可以看做是在Oxy平面內對坐標進行採樣得到的坐標對(x, y)
(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;
在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)生成三維數組,可用來計算三變數的函式和繪製三維立體圖
原始碼
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.
%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
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
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