edgarmtze
edgarmtze

Reputation: 25048

How would you normalize a histogram so the sum of each bin is 1?

How would you normalize a histogram A so the sum of each bin is 1

Dividing the histogram by the width of the bin, how do you draw it

I have this

dist        = rand(50)    
average     = mean(dist, 1);   
[c,x]       = hist(average, 15);    
normalized  = c/sum(c);
bar(x, normalized, 1)

In this case, n = 50,

The histogram must be close to the normal distribution.

Upvotes: 2

Views: 9544

Answers (1)

abcd
abcd

Reputation: 42225

That is a very unusual way of normalizing a probability density function. I assume you want to normalize such that the area under the curve is 1. In that case, this is what you should do.

[c,x]=hist(average,15);
normalized=c/trapz(x,c);
bar(x,normalized)

Either way, to answer your question, you can use randn to generate a normal distribution. You're now generating a 50x50 uniform distribution matrix and summing along one dimension to approximate a normal Gaussian. This is unnecessary. To generate a normal distribution of 1000 points, use randn(1000,1) or if you want a row vector, transpose it or flip the numbers. To generate a Gaussian distribution of mean mu and variance sigma2, and plot its pdf, you can do (an example)

mu=2;
sigma2=3;
dist=sqrt(sigma2)*randn(1000,1)+mu;
[c,x]=hist(dist,50);
bar(x,c/trapz(x,c))

Although these can be done with dedicated functions from the statistics toolbox, this is equally straightforward, simple and requires no additional toolboxes.

EDIT

I missed the part where you wanted to know how to generate a uniform distribution. rand, by default gives you a random variable from a uniform distribution on [0,1]. To get a r.v. from a uniform distribution between [a, b], use a+(b-a)*rand

Upvotes: 6

Related Questions