matlab - 如何标准化直方图

标签 matlab plot histogram distribution

我可以使用逆方法创建指数分布。我想标准化直方图。我该怎么做?

这是我的代码

N=100;
Lambda=2;
r=rand(N,1);
X=-log(1-r)/Lambda;
hist(X), colormap(bone);

t = 0:0.01:5;
pdf=Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2)

最佳答案

直方图应归一化为单位面积,以便与理论pdf进行比较。要归一化为单位面积,您需要除以样本数和 bin 宽度:

N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf

enter image description here

或者使用新的histogram函数(R2014b 中引入),根据指定的标准化选项自动标准化:

N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
histogram(X, 'Normalization', 'pdf') %// plot normalized histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf

关于matlab - 如何标准化直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33147545/

相关文章:

regex - 如何在 MATLAB 中使用 REGEXP 选择字符串的一部分或另一部分

R plot() 生成俄语日期标签

matlab - 如何在 Matlab 中绘制 3D 平面?

python - 与 MATLAB 的 imgaussfilt 相比,为什么使用 skimage 和 scipy 的二维高斯滤波之间的速度差异如此之大?

c# - 使用 Matlab dll 时,在 Windows Store App 中的模块 mscorlib.dll 错误中找不到类型 System.ApplicationException

r - 在两个 ggplot 直方图上显示均值和中值

java - 数组。直方图,将数组划分为子集并计算每组中的元素数量。 java

python - 如何在python中为非数字变量制作直方图

algorithm - 使用循环对数据进行分组(MATLAB 中的信号处理)

r - 在 R 中禁用绘图显示