1

如何在matlab中创建高斯分布的等高线图?

4

2 回答 2

3

具有对称 sigma 且中心位于 (0,0) 的简单情况:

sigma = 5;    % just an example value
n = 3*sigma;  % cutoff point
x = -n:n;
G = 1 / (sigma * sqrt(2 * pi)) * exp(-x.^2 / (2*sigma^2)); % 1D Gaussian
G2 = G' *  G; % 2D
contour(G2);  % make contour plot

或者在一般情况下,在任何地方都有一个中心,不同的 sigma x 和 sigma y 以及旋转 theta:

center = [1 3]; % again, example values
sigma = [4 2];
theta = pi/3;

R = max(sigma(:))*4;
[xgrid, ygrid] = meshgrid( center(1)-R: center(1)+R,...
                           center(2)-R: center(2)+R);

x = (((xgrid-center(1))*cos(theta) - (ygrid-center(2))*sin(theta))/sigma(1)).^2;
y = (((xgrid-center(1))*sin(theta) - (ygrid-center(2))*cos(theta))/sigma(2)).^2;

G = exp(-(x+y)/2);
contour( xgrid, ygrid, G)
于 2012-05-24T15:18:09.033 回答
1

The online documentation has everything you need. Check out the section for "Contour Graph of a Function"

There is example code, and all sorts of ways to label the contours, smooth them, etc.

于 2012-05-24T15:20:15.440 回答