简体   繁体   English

冲浪功能不会绘制2D高斯图形

[英]Surf function won't graph 2D Gaussian

I'm trying to graph a simple 2D Gaussian in MATLAB using the surf function, but I'm getting an error saying that the last value in surf must be a matrix and not a vector. 我正在尝试使用surf函数在MATLAB中绘制简单的2D高斯图形,但是却出现一个错误,指出surf中的最后一个值必须是矩阵而不是向量。 I don't understand how to graph the function then as every other example I've found while searching has had the third value as a vector. 我不明白该如何绘制函数的图形,因为我在搜索时发现的所有其他示例都将第三个值作为向量。 I feel as if I'm completely off base here with the surf function. 我觉得我在这里完全没有冲浪功能。 Any ideas? 有任何想法吗?

amp = 10;
x0 = 0;
y0 = 0;
sigmaX = 10
sigmaY = 10
X = 1:1:100;
Y = 1:1:100;
Z = amp*exp(-(X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2));
disp(size(Z))
surf(X, Y, Z);

Edit 编辑

When I plot this using @Suever's answer, I get something that doesn't look like a Gaussian at all. 当我使用@Suever的答案进行绘制时,我得到的东西看起来根本不像高斯。

Here's the plot 这是情节

amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
%X = 1:1:100;
%Y = 1:1:100;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-(X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2));
surf(X, Y, Z);

You have used X and Y to define a 2D domain over which you would like to compute your gaussian. 您已使用XY定义了一个2D域,您想在该2D域上计算高斯。 If you want Z to be a function of X and Y , you need to define Z for all permutations of X and Y . 如果希望ZXY的函数,则需要为XY 所有排列定义Z If you don't provide a matrix of Z values, MATLAB has no idea how to create a surface over the X Y ranges you've provided. 如果不提供Z值矩阵,则MATLAB不知道如何在提供的X Y范围内创建曲面。

You can create all permutations of X and Y using meshgrid and then compute Z over this entire domain. 您可以使用meshgrid创建XY所有排列,然后在整个域上计算Z Then you will be able to display the result with surf . 然后,您将可以使用surf显示结果。

amp = 10; x0 = 50; y0 = 50; sigmaX = 10; sigmaY = 10;

[X, Y] = meshgrid(1:100, 1:100);

% Z as you had it written (see correct version below)
Z = amp*exp(-(X-x0).^2./(2*sigmaX^2)+(Y-y0).^2./(2*sigmaY^2));

surf(X, Y, Z);

Update 更新资料

Your equation for the 2D Gaussian is wrong. 您的2D高斯方程式是错误的。 The - sign should be outside of the addition of the two components. -符号应位于两个组成部分的加法之外。 The way that you had it written, you negated the X-component and then added to the Y component. 编写方式是,否定X组件,然后将其添加到Y组件。

Z = amp*exp(-((X-x0).^2./(2*sigmaX^2)+(Y-y0).^2./(2*sigmaY^2)));

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM