如何在 MATLAB 中标准化直方图? [英] How to normalize a histogram in MATLAB?

查看:29
本文介绍了如何在 MATLAB 中标准化直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何归一化直方图,使得概率密度函数下的面积等于 1?

How to normalize a histogram such that the area under the probability density function is equal to 1?

推荐答案

我对此的回答与对您的 前面的问题.对于概率密度函数,整个空间的积分为 1.除以总和不会给你正确的密度.要获得正确的密度,您必须除以面积.为了说明我的观点,请尝试以下示例.

My answer to this is the same as in an answer to your earlier question. For a probability density function, the integral over the entire space is 1. Dividing by the sum will not give you the correct density. To get the right density, you must divide by the area. To illustrate my point, try the following example.

[f, x] = hist(randn(10000, 1), 50); % Create histogram from a normal distribution.
g = 1 / sqrt(2 * pi) * exp(-0.5 * x .^ 2); % pdf of the normal distribution

% METHOD 1: DIVIDE BY SUM
figure(1)
bar(x, f / sum(f)); hold on
plot(x, g, 'r'); hold off

% METHOD 2: DIVIDE BY AREA
figure(2)
bar(x, f / trapz(x, f)); hold on
plot(x, g, 'r'); hold off

您可以亲眼看看哪种方法与正确答案(红色曲线)一致.

You can see for yourself which method agrees with the correct answer (red curve).

另一种对直方图进行归一化的方法(比方法 2 更直接)是除以表示概率密度函数积分的 sum(f * dx),即

Another method (more straightforward than method 2) to normalize the histogram is to divide by sum(f * dx) which expresses the integral of the probability density function, i.e.

% METHOD 3: DIVIDE BY AREA USING sum()
figure(3)
dx = diff(x(1:2))
bar(x, f / sum(f * dx)); hold on
plot(x, g, 'r'); hold off

这篇关于如何在 MATLAB 中标准化直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆