Scale a histogram to get area = 1
Mostrar comentarios más antiguos
Hi, I just want to know how to scale a histogram so that the total area is equals to 1.
Respuestas (4)
David Young
el 3 de Mzo. de 2011
If equally-spaced bins are OK, you can do this:
% some data
d = randn(10000, 1);
%compute histogram
nbins = 50;
[h, centres] = hist(d, nbins);
% normalise to unit area
norm_h = h / (numel(d) * (centres(2)-centres(1)));
plot(centres, norm_h);
5 comentarios
Walter Roberson
el 3 de Mzo. de 2011
I was going to give the histc() equivalent, but realized that it might not be well defined. With histc() the last output bin counts the elements that are exactly equal to the last edge, but the width of that bin is 0 and thus so is the area. If you get a non-zero count there, then you cannot calculate a proper normalization.
Jan
el 3 de Mzo. de 2011
@Walter: Would your HISTC approach work if the last bin is set to Inf?
Walter Roberson
el 3 de Mzo. de 2011
Maybe. I'm still waking up so I'll think about that later.
Jan
el 3 de Mzo. de 2011
@Walter: Good morning.
Walter Roberson
el 3 de Mzo. de 2011
The calculation I was thinking of last night was:
h = histc(d, edges);
norm_h = h(1:end-1) ./ (h(1:end-1) * diff(edges).');
Note that that is a matrix multiply, not an element-wise multiply.
A quick numeric examination shows that if the final edge were inf then the final diff() result would be inf, thus giving an infinite area to the h(end-1) .* (edges(end) - edges(end-1)) portion of the matrix multiplication, which is Not Good.
I'll have to rethink the normalization.
Jan
el 9 de Mzo. de 2011
1 voto
Ayham Alsaoud
el 12 de Oct. de 2019
h = histogram(data,"Normalization","pdf");
This is exactly the feature of the probability density function of the histogram (It has area of 1 and stil do the scaling that you wanted)
%% To calculate the Area and try it :
h = histogram(data,"Normalization","pdf");
edges = h.BinEdges;
v = h.Values;
a = 0;
for i =1:size(v,2)
a = a + v(i);
end
area = (edges(2)-edges(1))*s;
2 comentarios
Alaa Al-jobory
el 13 de Mayo de 2020
Thanks this is what i want...
Walter Roberson
el 13 de Mayo de 2020
This is good now; sadly back in 2011 when the original question was asked, Normalization was not an option for histogram()
Alaa Al-jobory
el 14 de Mayo de 2020
0 votos
but i still have problem i am not need to use histogram(data,"Normalization","pdf") i have an equation p(S)=exp(-(S-S_0 )^2/2σ^2 )/√(2πσ^2 ) i try to polt it like histogram(data,"Normalization","pdf") but it give me different result
Thanks in advance
Categorías
Más información sobre Histograms en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!