필터 지우기
필터 지우기

Histogram with adjusted bins to gaussian

조회 수: 2 (최근 30일)
daniel caltrider
daniel caltrider 2020년 5월 16일
댓글: Tommy 2020년 5월 19일
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

답변 (1개)

Tommy
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
daniel caltrider
daniel caltrider 2020년 5월 16일
I tried the code but got this error
Check for missing argument or incorrect argument data type in call to function 'fit'.
Tommy
Tommy 2020년 5월 19일
Hmm... what is your MATLAB version and what does this print?
which fit -all

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by