I have a bivariate gaussian I defined as follow:
我有一个二元高斯函数定义如下:
I=[1 0;0 1];
mu=[0,0];
sigma=0.5*I;
beta = mvnrnd(mu,sigma,100); %100x2 matrix where each column vector is a variable.
now I want to plot a contour of the pdf of the above matrix. What I did:
现在我想画出上面这个矩阵的pdf的轮廓。我所做的:
Z = mvnpdf(beta,mu,sigma); %100x1 pdf matrix
Now I want to plot a contour of the bivariate gaussian beta. I know I should use the command contour but this one require Z to be a square matrix. how do I solve this? I am very confused and not sure how to plot the contour of the bivariate gaussian!! ANY HELP IS GREATLY APPRECIATED..
现在我想画一个二元高斯函数的轮廓。我知道我应该使用命令轮廓但是这个要求Z是一个方阵。怎么解呢?我很困惑,不知道如何画出二元高斯的轮廓!!我非常感激你的帮助。
Thank you
谢谢你!
1 个解决方案
#1
14
You need to define your x
, y
axes and use meshgrid
(or ndgrid
) to generate all combinations of x, y values, in the form of two matrices X
and Y
. You then compute the Z
values (your Gaussian pdf) for those X
and Y
, and plot Z
as a function of X
, Y
using contour
(contour plot), or perhaps surf
(3D plot).
你需要定义x,y轴和使用meshgrid(或ndgrid)生成所有组合的x,y值,对于x、y两个矩阵的形式然后计算Z值(高斯pdf)的x和y,Z的函数和情节x,y使用轮廓(等高线图),或者冲浪(三维图)。
mu = [0,0]; %// data
sigma = [.5 0; 0 .5]; %// data
x = -5:.1:5; %// x axis
y = -4:.1:4; %// y axis
[X Y] = meshgrid(x,y); %// all combinations of x, y
Z = mvnpdf([X(:) Y(:)],mu,sigma); %// compute Gaussian pdf
Z = reshape(Z,size(X)); %// put into same size as X, Y
%// contour(X,Y,Z), axis equal %// contour plot; set same scale for x and y...
surf(X,Y,Z) %// ... or 3D plot
#1
14
You need to define your x
, y
axes and use meshgrid
(or ndgrid
) to generate all combinations of x, y values, in the form of two matrices X
and Y
. You then compute the Z
values (your Gaussian pdf) for those X
and Y
, and plot Z
as a function of X
, Y
using contour
(contour plot), or perhaps surf
(3D plot).
你需要定义x,y轴和使用meshgrid(或ndgrid)生成所有组合的x,y值,对于x、y两个矩阵的形式然后计算Z值(高斯pdf)的x和y,Z的函数和情节x,y使用轮廓(等高线图),或者冲浪(三维图)。
mu = [0,0]; %// data
sigma = [.5 0; 0 .5]; %// data
x = -5:.1:5; %// x axis
y = -4:.1:4; %// y axis
[X Y] = meshgrid(x,y); %// all combinations of x, y
Z = mvnpdf([X(:) Y(:)],mu,sigma); %// compute Gaussian pdf
Z = reshape(Z,size(X)); %// put into same size as X, Y
%// contour(X,Y,Z), axis equal %// contour plot; set same scale for x and y...
surf(X,Y,Z) %// ... or 3D plot