Problem when plotting the figure
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2020-9-6
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 个评论
Star Strider
2020-9-6
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.)
.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!