Scale a histogram to get area = 1

17 views (last 30 days)
Joseph Lee
Joseph Lee on 3 Mar 2011
Answered: Alaa Al-jobory on 14 May 2020
Hi, I just want to know how to scale a histogram so that the total area is equals to 1.

Answers (4)

David Young
David Young on 3 Mar 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 Comments
Jan
Jan on 3 Mar 2011
@Walter: Good morning.
Walter Roberson
Walter Roberson on 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.

Sign in to comment.


Jan
Jan on 9 Mar 2011

Ayham Alsaoud
Ayham Alsaoud on 12 Oct 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 Comments
Alaa Al-jobory
Alaa Al-jobory on 13 May 2020
Thanks this is what i want...
Walter Roberson
Walter Roberson on 13 May 2020
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

Community Treasure Hunt

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

Start Hunting!