Scale a histogram to get area = 1

Hi, I just want to know how to scale a histogram so that the total area is equals to 1.

Réponses (4)

David Young
David Young le 3 Mar 2011

1 vote

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 commentaires

Walter Roberson
Walter Roberson le 3 Mar 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
Jan le 3 Mar 2011
@Walter: Would your HISTC approach work if the last bin is set to Inf?
Walter Roberson
Walter Roberson le 3 Mar 2011
Maybe. I'm still waking up so I'll think about that later.
Jan
Jan le 3 Mar 2011
@Walter: Good morning.
Walter Roberson
Walter Roberson le 3 Mar 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.

Connectez-vous pour commenter.

Ayham Alsaoud
Ayham Alsaoud le 12 Oct 2019

1 vote

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 commentaires

Alaa Al-jobory
Alaa Al-jobory le 13 Mai 2020
Thanks this is what i want...
Walter Roberson
Walter Roberson le 13 Mai 2020
This is good now; sadly back in 2011 when the original question was asked, Normalization was not an option for histogram()

Connectez-vous pour commenter.

Alaa Al-jobory
Alaa Al-jobory le 14 Mai 2020

0 votes

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

Community Treasure Hunt

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

Start Hunting!

Translated by