フィルターのクリア

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 Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by