Problem when plotting the figure

2 ビュー (過去 30 日間)
yusra Ch
yusra Ch 2020 年 9 月 6 日
コメント済み: Star Strider 2020 年 9 月 6 日
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

採用された回答

Star Strider
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 件のコメント
yusra Ch
yusra Ch 2020 年 9 月 6 日
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 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 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by