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

查看:82
本文介绍了如何在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天全站免登陆