Histogram with adjusted bins to gaussian
조회 수: 2 (최근 30일)
이전 댓글 표시
I need to take the new histogram from this code and apply a gaussian but when I try to do so I get a gaussian over the unaltered histogram.
here is my code
[num,~,~]=xlsread('histogram2.xlsx');
d=num;
h = histogram(d,50)
%remove one count per bin for background estimate
h.BinCounts(h.BinCounts>0) = h.BinCounts(h.BinCounts>0)-1
h=histfit(d,50)
I dont know how to referance the histogram with the adjusted bins in the histfit function
I am on R2020a
댓글 수: 0
답변 (1개)
Tommy
2020년 5월 16일
If you'd like to fit a histogram to a normal distribution but you don't know the underlying data (e.g. you've altered the histogram in some way), you could instead fit the bin centers and values to a Gaussian curve:
% create your histogram
[num,~,~]=xlsread('histogram2.xlsx');
d=num;
h = histogram(d,50)
h.BinCounts(h.BinCounts>0) = h.BinCounts(h.BinCounts>0)-1
% fit the Gaussian, based only on h
x = h.BinEdges(1:end-1)+h.BinWidth/2;
y = h.Values;
gaussian = @(mu, sig, scale, x) 1/(sig*sqrt(2*pi))*exp(-(((x-mu)/sig).^2)/2) * scale;
x0 = [mean(x), range(x), sum(h.Values*h.BinWidth)]; % guesses for [mu, sig, scale]
f = fit(x(:), y(:), gaussian, 'StartPoint', x0);
% plot the Gaussian on top of your histogram
hold on;
p = plot(f);
p.LineWidth = 2;
You can obtain the fit parameters with f.mu and f.sig. You may need to use a better starting point.
댓글 수: 2
Tommy
2020년 5월 19일
Hmm... what is your MATLAB version and what does this print?
which fit -all
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!