Question

I'm trying to plot a gaussian mixture model using matlab. I'm using the following code/data:

p = [0.048544095760874664 , 0.23086205172287944 , 0.43286598287228106 ,0.1825503345829704 , 0.10517753506099443];

meanVectors(:,1) = [1.3564375381318807 , 5.93145751073734];
meanVectors(:,2) = [3.047518052924292 , 3.0165339699001463];
meanVectors(:,3) = [7.002335595122265 , 6.02432823594635];
meanVectors(:,4) = [6.990841095715846 , 3.5056707068971438];
meanVectors(:,5) = [6.878912868397179 , 1.1054191293515965];

covarianceMatrices(:,:,1) = [1.3075839191466305 0.07843065902827488; 0.07843065902827488 0.3167448334937619];
covarianceMatrices(:,:,2) = [0.642914957488056 0.15638677636129855; 0.15638677636129852 0.382240356677974];
covarianceMatrices(:,:,3) = [0.8216051423486987 0.15225179380145448; 0.15225179380145445 0.37030472711188295];
covarianceMatrices(:,:,4) = [1.064002437166605 0.11798234162403692; 0.11798234162403692 0.2687495955430368];
covarianceMatrices(:,:,5) = [0.6445011493286044 0.15295220981440236; 0.1529522098144023 0.5231676196736254];

obj = gmdistribution(meanVectors', covarianceMatrices, p);

figure(1);
ezcontour(@(x,y)pdf(obj,[x y]), [-10 10], [-10 10]);

figure(2);
ezsurf(@(x,y)pdf(obj,[x y]), [-10 10], [-10 10]);

But the resulting surface appears to be really "spiky". Am i doing something wrong?

Was it helpful?

Solution

It seems that the problem is ploting the function This piece of code is much slower, but it works for me

p = [0.048544095760874664 , 0.23086205172287944 , 0.43286598287228106 ,0.1825503345829704 , 0.10517753506099443];

meanVectors(:,1) = [1.3564375381318807 , 5.93145751073734];
meanVectors(:,2) = [3.047518052924292 , 3.0165339699001463];
meanVectors(:,3) = [7.002335595122265 , 6.02432823594635];
meanVectors(:,4) = [6.990841095715846 , 3.5056707068971438];
meanVectors(:,5) = [6.878912868397179 , 1.1054191293515965];

covarianceMatrices(:,:,1) = [1.3075839191466305 0.07843065902827488; 0.07843065902827488 0.3167448334937619];
covarianceMatrices(:,:,2) = [0.642914957488056 0.15638677636129855; 0.15638677636129852 0.382240356677974];
covarianceMatrices(:,:,3) = [0.8216051423486987 0.15225179380145448; 0.15225179380145445 0.37030472711188295];
covarianceMatrices(:,:,4) = [1.064002437166605 0.11798234162403692; 0.11798234162403692 0.2687495955430368];
covarianceMatrices(:,:,5) = [0.6445011493286044 0.15295220981440236; 0.1529522098144023 0.5231676196736254];

obj = gmdistribution(meanVectors', covarianceMatrices, p);

x = -10:.2:10; y = x; n=length(x); a=zeros(n,n);
for i = 1:n, for j = 1:n, a(i,j) = pdf(obj,[x(i) y(j)]); end, end;
surf(x,y,a,'FaceColor','interp','EdgeColor','none','FaceLighting','phong')

OTHER TIPS

The problem is the default grid size, which is 60. Set a higher number and you will get the expected result:

figure(1);
ezcontour(@(x,y)pdf(obj,[x y]), [-10 10], [-10 10],300);

figure(2);
ezsurf(@(x,y)pdf(obj,[x y]), [-10 10], [-10 10],300);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top