Bivariate Guassian mixed model on 1D data
2 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to plot a guassian curve on the histogram distribution of my 1D data. This is my code:
data = xlsread('csf.xlsx','ab');
mu1 = mean('data');
sigma1 = std('data');
X = [mvnrnd(mu1,sigma1,1000)];
numComponents=2;
paramEsts = fitgmdist(X,numComponents);
MU=[paramEsts.mu(1);paramEsts.mu(2)];
SIGMA=cat(3,[paramEsts.Sigma(1)],[paramEsts.Sigma(2)]);
PPp=[paramEsts.PComponents(1),paramEsts.PComponents(2)];
gm = gmdistribution(MU,SIGMA,PPp);
dimension = gm.NumVariables
rng(1);
min_val = min(data(:)-100); max_val = max (data(:)+1000);
% min_val = min('data'); max_val = max ('data');
histogram(data, 'Normalization', 'pdf', 'EdgeColor', 'none')
x= min_val:0.01:max_val;
xlim([min_val max_val])
hold on;
xgrid = linspace (50,3000,500)';
n1 = makedist('normal',paramEsts.mu(1),sqrt(paramEsts.Sigma(1)));
n2 = makedist('normal',paramEsts.mu(2),sqrt(paramEsts.Sigma(2)));
p = paramEsts.ComponentProportion;
y = p(1)*pdf(n1,xgrid) + p(2)*pdf(n2,xgrid);
hold on; plot(xgrid,y,'b'); hold off
And this is what I get:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194352/image.jpeg)
However, I am trying to get something like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194353/image.png)
Please, how do I achieve this?
2 个评论
the cyclist
2018-8-24
Can you upload either the Excel file or the resulting data variable in a MAT file (using that little paperclip icon)?
回答(1 个)
Image Analyst
2018-8-24
Try something like the attached demo that fits two Gaussians to data. Adapt as needed (like replace blue dots on plot with white bars or whatever).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194212/image.png)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!