您将如何对直方图进行归一化,以便每个bin的总和为1? [英] How would you normalize a histogram so the sum of each bin is 1?
问题描述
如何对直方图A
进行归一化,使每个bin的总和为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
我有这个
dist = rand(50)
average = mean(dist, 1);
[c,x] = hist(average, 15);
normalized = c/sum(c);
bar(x, normalized, 1)
在这种情况下,n = 50
- 获取值的公式是什么
均值和方差^ 2?我们写
N(mean, (variance^2) / 50)
,但是如何? - 如何绘制均匀分布和正态分布?
- What is it the formula to get values
of mean and variance^2? We write
N(mean, (variance^2) / 50)
, but how? - How do you plot both uniform distribution and normal distribution?.
直方图必须接近正态分布.
The histogram must be close to the normal distribution.
推荐答案
这是标准化概率密度函数的一种非常不寻常的方法.我假设您要归一化,以使曲线下的面积为1.在这种情况下,这就是您应该做的.
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)
无论哪种方式,要回答您的问题,都可以使用randn
生成正态分布.现在,您将生成一个50x50
均匀分布矩阵,并沿一维求和以近似正态高斯.这是没有必要的.要生成1000个点的正态分布,请使用randn(1000,1)
;如果需要行向量,请对其进行转置或翻转数字.要生成均值mu
和方差sigma2
的高斯分布,并绘制其pdf,可以这样做(一个示例)
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.
编辑
我错过了您想知道如何生成均匀分布的部分. rand
,默认情况下为您提供[0,1]
上均匀分布的随机变量.取得R.V.从[a, b]
之间的均匀分布中,使用a+(b-a)*rand
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
这篇关于您将如何对直方图进行归一化,以便每个bin的总和为1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!