在 MATLAB 中绘制 3D 高斯运算符(λ 1 = λ 2 >> λ 3)



我的目标是在 matlab 中绘制 3D 高斯算子。现在我有了我的方程,我认为运算符应该是扁球体(对于我们有大的 lambda1 和 lambda2 与小 lambda 3 相比的情况)。以下是我这样做的尝试。我尝试以两种不同的方式做到这一点:首先使用网格,或者通过构建矩阵本身。用于绘图的命令等值面是我在网上获得的适用于 3D 绘图的东西。非常感谢您对此的评论和建议

    %% plot the gaussian 
clc
clear all
close all

 % define the variances (sigma1 , sigma2 and sigma3) which are essentially the three lambdas with lambda 1 and two close to each other and a small lambda 3. 
lambda1 = 3;
lambda2 = 2.5;
lambda3 = 0.5;
anorm=1/( (2.0*pi)^1.5 * sqrt( lambda1*lambda2*lambda3 ) );
%% building the matrix 
% I used this grid (from -5 : 4) to center the gaussian in the middle 
for x1=-5:4
    for x2=-5:4
        for x3=-5:4
g(x1+6,x2+6,x3+6) = anorm * exp(-0.5*((x1^2/lambda1)+(x2^2/lambda2)+(x3^2/lambda3))); 
        end 
    end 
end
%% second attempt
[i, j, k] = meshgrid(-5:4 , -5:4 ,-5:4); 
Gauss = anorm * exp(-0.5*((i.^2/lambda1)+(j.^2/lambda2)+(k.^2/lambda3)));
%% plotting
figure (1) 
isosurface(g)
grid on
xlabel('X', 'FontSize', 14)
ylabel('Y', 'FontSize', 14)
zlabel('Z', 'FontSize', 14)
figure (2) 
isosurface(Gauss)
grid on
xlabel('X', 'FontSize', 14)
ylabel('Y', 'FontSize', 14)
zlabel('Z', 'FontSize', 14)
好吧,

确实用第二个图生成了 3D 等值面。

看起来您还发现 MATLAB 的 3D 绘图选项有限。当我有 3D 数据时,我通常更喜欢查看各种切片,这在下面的脚本中实现。

希望这有帮助!

%XY contours of 3d gaussian
figure
hold on
size_mat=100;   %number of grid elements
max_x=3;        %maxmimum spread in x and y
gg=zeros(size_mat, size_mat);   %gaussion function
fixed_z=5;      %fix z while taking this contour
delta_x=max_x/size_mat*2;   %spacing between grid points
%loop over grid
for i=1:size_mat
    for j=1:size_mat
        cur_x=-max_x+i*delta_x;
        cur_y=-max_x+j*delta_x;
        %computed value of 3d gaussian
        gg(i,j) = anorm * exp(-0.5*((cur_x.^2/lambda1)+(cur_y.^2/lambda2)*(fixed_z^2/lambda3)));
    end
end
%make contour plot
h=contour(gg)
%clabel(h)
title('Contour of 3d Gaussian with z=5')

最新更新