Problem when plotting the figure

2 views (last 30 days)
I have a vector called K [622x1] double I plot the PDF of K . I fitted my K vector to the Gaussian mixture distribution and ploted the PDF with a solid line :
>> GMModel = fitgmdist(k,2);
>> gm=gmdistribution(GMModel.mu,GMModel.Sigma);
>> pdfk=pdf(gm,k);
>> figure
>> plot(k,pdfk)
The plot has several lines and I dont know what is the problem and how to resolve it. Ca anyone help me with this? thank youuu

Accepted Answer

Star Strider
Star Strider on 6 Sep 2020
I do not have ‘k’, so I created my own, and was able to reproduce essentially the result you got.
The multiple lines disappear of you sort ‘k’ first:
k = sum([randn(622,1)+5; randn(622,1)+1],2);
k = sort(k); % <— ADD ‘sort’ CALL
GMModel = fitgmdist(k,2);
gm=gmdistribution(GMModel.mu,GMModel.Sigma);
pdfk=pdf(gm,k);
figure
plot(k,pdfk)
.
  4 Comments
yusra Ch
yusra Ch on 6 Sep 2020
I have one more question, How to draw the PDF of the vector K ?
I have plotted the PDF fitted to the mixture Gaussian distribution and I want to compare the two plots.
Star Strider
Star Strider on 6 Sep 2020
I would do something like this (starting with my original code):
k = sum([randn(622,1)+5; 0.5*randn(622,1)+1],2);
k = sort(k); % <— ADD ‘sort’ CALL
GMModel = fitgmdist(k,2);
gm=gmdistribution(GMModel.mu,GMModel.Sigma);
pdfk=pdf(gm,k);
[f,x] = ecdf(k); % Empirical Cumulative Distribution Function
[fr,xr] = resample(f, x); % Resample To Constant Sampling Interval
df = gradient(fr)./gradient(xr); % Calculate Derivative To Get PDF
dfs = smoothdata(df, 'gaussian', 150); % Smooth To Eliminate Noise
figure
plot(k,pdfk)
hold on
plot(xr, dfs)
hold off
This appears to produce a reasonable approximation. Experiment with the smoothdata function (introduced in R2017a) with your data to get different results. (I do not kinow what MATLAB version you are using. Other options, such as movmedian to do the smoothing were introduced in R2016a, and of course there are still other options.)
.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!