做者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/函數
%% demo Multivariate Normal Distribution clear clc %% 空間座標範圍 x1=-5:0.2:5; x2=-5:0.2:5; [X1, X2]=meshgrid(x1, x2); X=[X1(:) X2(:)]; %% 高斯分佈參數 % 份量1 miu_1=[1 1]; % 均值 Sigma_1=[2 -2;-2 3]; % 協方差 % 份量2 miu_2=[0 -2]; % 均值 Sigma_2=[5 0.5;0.5 1]; % 協方差 %% 高斯機率密度函數 % 份量1 y_1=mvnpdf(X, miu_1, Sigma_1); y_1=reshape(y_1, length(x2), length(x1)); % 份量2 y_2=mvnpdf(X, miu_2, Sigma_2); y_2=reshape(y_2, length(x2), length(x1)); %% 2D密度圖 figure(1); contour(x1, x2, y_1); hold on contour(x1, x2, y_2); xlabel('x1'); ylabel('x2'); saveas(gcf,sprintf('Gauss_2D.jpg'),'bmp'); %% 3D密度圖 figure(2); surf(x1, x2, y_1); hold on surf(x1, x2, y_2); xlabel('x1'); ylabel('x2'); zlabel('Probability Density'); saveas(gcf,sprintf('Gauss_3D.jpg'),'bmp');