今天在看点目标成像仿真程序的时候,看到了meshgrid函数,看了matlab的帮助文档后理解了一点,特此记录学习过程。

目录

  • 一、meshgrid函数
  • 二、举例验证
  • 三、创建二维网格绘制曲面图
  • 四、总结
  • 五、meshgrid函数源代码(仅供参考):

一、meshgrid函数

meshgrid函数是MATLAB中用于生成网格采样点数的函数,通常进行2D、3D图形的绘制。

1、【X,Y】 = meshgrid(x,y) :基于向量x和y中包含的坐标返回二维网格坐标。X是一个矩阵,每一行是x的一个副本,Y也是一个矩阵,每一列是y的一个副本。坐标X和Y表示的网格有length(y)个行和length(x)个列。

2 、[X,Y] = meshgrid(x) 与 [X,Y] = meshgrid(x,x)相同,返回网格大小为length(x)*length(x)的方形网格矩阵。

3、 [X,Y,Z] = meshgrid(x,y,z),返回由向量x,y,z定义的三维网格坐标,X,Y和Z表示的网格大小为length(x)*length(y)*length(z)。


二、举例验证

1.【X,Y】 = meshgrid(x,y) , 代码如下:

a 、b矩阵个数相同:

a = [1 2 3 4];
b = [5 6 7 8];
[A,B] = meshgrid(a,b)

结果:

A =1     2     3     41     2     3     41     2     3     41     2     3     4B =5     5     5     56     6     6     67     7     7     78     8     8     8>>

a 、b矩阵数量不同:

a = [1 2 3];
b = [4 5 6 7];
[A,B] = meshgrid(a,b)
A =1     2     31     2     31     2     31     2     3B =4     4     45     5     56     6     67     7     7>> 

2、【X,Y】 = meshgrid(x), 代码如下:

x  = [1 2 3];
[X,Y] = meshgrid(x)
X =1     2     31     2     31     2     3Y =1     1     12     2     23     3     3
>>
x = [1 2 3];
>> [X,Y] = meshgrid(x,x)X =1     2     31     2     31     2     3Y =1     1     12     2     23     3     3>>

三、创建二维网格绘制曲面图

使用均匀分布的x和y坐标在-4到4之间创建二维网格:

代码如下:

x = -4:0.2:4;
y = x;
[X,Y] = meshgrid(x);
F = X.*exp(-X.^2 - Y.^2);
surf(X,Y,F);


四、总结

为什么要使用meshgrid?
matlab使用矩阵的方式进行运算,对于2D而言,如果采样10个点(指x,y轴),那么对于x=第一个采样点,反映到矩阵就是10个,即不管y是哪个值,x的第一采样点保持不变;对y是同理。因此,2D产生的x和y都是两维矩阵。


五、meshgrid函数源代码(仅供参考):

源代码:
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

[MATLAB]中meshgrid函数的用法与实践(学习笔记)相关推荐

  1. matlab中 meshgrid 函数的用法

    转自:https://blog.csdn.net/foreverhuylee/article/details/32731349 meshgrid是MATLAB中用于生成网格采样点的函数.在使用MATL ...

  2. matlab roundn函数_columns函数的使用方法 matlab中round函数具体用法

    Excel中column函数的使用方法是什么?其实小编会说分手是想被挽留,你却顺口祝小编自由. 只读属性,返回 TextStream 文件中当前字符位置的列号. 语法: =Column(referen ...

  3. MATLAB中randi函数的用法

    MATLAB中randi函数的用法 均匀分布的伪随机整数 语法 X = randi(imax) X = randi(imax,n) X = randi(imax,sz1,...,szN) X = ra ...

  4. MATLAB中freqz函数的用法

    本文引用自乱七八糟<MATLAB中freqz函数的用法>  FREQZ 是计算数字滤波器的频率响应的函数 [H,W] = FREQZ(B,A,N)returns the N-point c ...

  5. Matlab中linprog函数的用法总结

    Matlab中 linprog函数的用法总结 1.简介 在matlab中,linprog函数可以求解线性规划问题,用于寻找目标函数的最小值 matlab中,规划模型的标注写法如下 \[ min\ f\ ...

  6. Matlab中min函数的用法

    Matlab中min函数的用法 向量中的最小元素 创建一个向量并计算其最小元素. M = min(A) A = [23 42 37 15 52]; M = min(A)M = 15 每个矩阵列中的最小 ...

  7. MATLAB中plot()函数的用法

    文章目录 前言 一.plot()函数 二.代码运行结果 总结 前言 此为本人学习中的一些笔记总结,仅供参考. 提示:以下是本篇文章正文内容,下面案例可供参考 一.plot()函数 提示:关于plot( ...

  8. matlab的filter函数,filter函数用法 matlab中filter函数的用法

    matlab中filter函数的用法如果你深爱的人此刻在你身边陪你,你怎么会有时间来看这些文字呢 离散系统的差分方程为 2y[k]-y[-1]-3y[k-2]=2x[k]-x[k-1] x[k]=(0 ...

  9. matlab中reshape函数的用法

    matlab中reshape函数的用法 1.函数功能:变换矩阵维度 2.转换规律:按列读,按列存 A =[1, 2, 3, 4;5, 6, 7, 8;9, 10, 11, 12 ];C = resha ...

最新文章

  1. 数据库 SQL 优化大总结之:百万级数据库优化方案
  2. linux的周期计划任务叫atd,linux计划任务
  3. 自动给文本框输入值_Dynamo for Revit自动生成门窗图例详图
  4. 不同php怎么传递参数,php – 将所有参数传递给另一个函数
  5. ABAP WRITE
  6. Python笔记-方差分析之单因素方差分析
  7. html里面textfield属性,StyleableTextField的CSS属性htmlText
  8. Word2016怎么和mathtype兼容
  9. js打开新窗口的方法
  10. 机器码、序列号、认证码、注册码的生成算法(一)
  11. 采用WPF开发第二版OFD阅读器,持续完善中,敬请期待
  12. 【汽车电子】嵌入式软件开发常用工具
  13. 安卓手机运行springboot 应用,做java 服务器
  14. 教大家做蛋黄酥的做法
  15. wamp5环境配置基础教程
  16. python|爬虫东宫小说
  17. UML软件建模技术-基于IBM RSA工具的基础实训
  18. 为什么建议iOS开发使用Swift
  19. sparksql语法,读json
  20. IllegalArgumentException 异常

热门文章

  1. 智能家居的发展现状以及未来发展趋势的分析
  2. Nisi实现安装包制作
  3. 三、线性规划 单纯形法
  4. vue项目启动后自动跳转页面
  5. 网购秒杀系统架构设计 1
  6. vscode撤销快捷键
  7. 5. KFold, StratifiedKFold,StratifiedShuffleSplit, GroupKFold区别以及Stratified Group KFold
  8. keyboard hook
  9. Linux内核可自己增加吗6,一篇最完善可行的Linux 2.6.10内核升级文档Linux -电脑资料...
  10. TensorFlow 复制placeholder