MATLAB Answers

Ploting the result of a fitgmdist (of 1D data)

31 views (last 30 days)
I've seen something similar answered a few times here, but none of the solutions work for me.
I have 1D data that I want to fit as 3 Gaussian functions. So, I do a simple:
Fit=fitgmdist(AbsY,3);
The problem now is that I cannot display this. One of the answers on here proposed something like this:
figure
plot(AbsX,AbsY)
hold on
ezcontour(@(x,y)pdf(Fit,[x y]))
But this produces all kinds of errors.
The examples I've seen on the web use the pdf function, but I don't really understand what the second argument should be? Is that the x values of my series? Or the peaks of my Gaussians? Or something else? Also, do I plot it with 'plot', or 'ezcontour' or something else?
Any help with this would be greatly appreciated.
P.S. Maybe I'm too beginner to understand, but the web page for this function is super useles... It only gives examples with contour plots and 2D, and for visualization the first example is:
gmPDF = @(x1,x2)reshape(pdf(GMModel,[x1(:) x2(:)]),size(x1));
g = gca;
fcontour(gmPDF,[g.XLim g.YLim])
Which I cannot make heads or tails of... What are x1 and x2? What does the @ at the start do? Where did g.XLim and g.YLim come from?

  0 Comments

Sign in to comment.

Accepted Answer

Jeff Miller
Jeff Miller on 27 May 2020
Say you want to plot the distribution from -15 to 15 in steps of 0.05. This would be the code:
z = (-15:0.05:15)'; % Note the crucial '
pdfz = pdf(Fit,z);
figure;
plot(z,pdfz);
Just fill in your desired range and step size instead of -15, 15, and 0.05

  2 Comments

Marin Vojkovic
Marin Vojkovic on 27 May 2020
I see, I think I was missing the ' there. Thank you.
I have another question now, as I'm suspecting I'm using this wrong. When I put the desired range as part of my x range, what I get is nowhere near what I need/thought I'd get.
This is my output:
Blue is the data, red is the "fit". I want to fit three Gaussians that would represent the data. Maybe this is not the bast method?
Also, if I've used
Fit=fitgmdist(AbsY,3);
why do I get only one output from pdf?
Jeff Miller
Jeff Miller on 27 May 2020
To get the blue and red curves on the same scale, plot the blue histogram with an option like 'Normalization','pdf' (from memory). The pdfz values your plotting integrate to one, as they should, but the integral of the blue curve looks to be millions. In essence, pdfs are not sensitive to sample size, but histograms are.

Sign in to comment.

More Answers (0)