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.

Answers (4)

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 Comments

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

Sign in to comment.

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 Comments

Thanks this is what i want...
This is good now; sadly back in 2011 when the original question was asked, Normalization was not an option for histogram()

Sign in to comment.

Alaa Al-jobory
Alaa Al-jobory on 14 May 2020
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

Asked:

on 3 Mar 2011

Answered:

on 14 May 2020

Community Treasure Hunt

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

Start Hunting!