Borrar filtros
Borrar filtros

Histogram with adjusted bins to gaussian

2 visualizaciones (últimos 30 días)
daniel caltrider
daniel caltrider el 16 de Mayo de 2020
Comentada: Tommy el 19 de Mayo de 2020
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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by