meshgrid()函数

函数功能

假设有两个向量x=[1:3], y=[1:2],通过meshgrid()函数我们可以得到
在这里插入图片描述
假设有三个向量x=[1:3],y=[4:5],z=[6:10],通过meshgrid()函数我们可以得到

m的三维表示:
在这里插入图片描述
在这里插入图片描述
n的三维表示:
在这里插入图片描述
在这里插入图片描述
l的三维表示:
在这里插入图片描述
在这里插入图片描述

函数源码

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
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值