Normalizing Histogram but with a line instead of bars

Hi,
I have a 20,000*1 data set called z and I am using bins (spacing) of w=-10:0.01:10.
So I can do a regular histogram plot:
hist(z,w)
But I would like to plot it as a line so I use:
[n1,wout1]=hist(z,w);
plot(wout1,n1)
But this line isn't normalised, can someone tell how to normalise my data? (Notice that the data is necessarily from randn)
Thanks!

3 Comments

What do you mean by normalized? How can a line be normalized? Do you mean that you want the counts normalized, instead of the line, like if you divided by the max count, or the sum of all counts or something?
I wish for the area to equal 1?
Ryan
Ryan on 20 Jul 2012
Edited: Ryan on 20 Jul 2012
Divide n1 by the sum(z).

Sign in to comment.

Answers (2)

How about:
[n1,wout1]=hist(z,w);
% Normalize so that the sum of the points = 1.
normalizedCounts = n1 / sum(n1);
plot(wout1, normalizedCounts);
But that only normalizes vertically. The area under the curve is the sum of the y values times the spacing in the x direction (the bin width). To get an area of 1 (which I don't know why you'd want and I don't see any use for), you'd have to divide by the total area.
% Get total area under the curve so far.
deltaX = wout1(2) - wout1(1);
totalArea = sum(n1) * deltaX;
% Normalize so that the area under the curve = 1.
normalizedCounts = n1 / totalArea;
plot(wout1, normalizedCounts);

Asked:

on 20 Jul 2012

Community Treasure Hunt

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

Start Hunting!