自2014b起,Matlab将这些规范化例程本机嵌入在histogram函数中(有关此函数提供的6个例程,请参阅帮助文件)。这是一个使用PDF归一化的示例(所有bin的总和为1)。
data = 2*randn(5000,1) + 5; % generate normal random (m=5, std=2)
h = histogram(data,'Normalization','pdf') % PDF normalization
对应的PDF是
Nbins = h.NumBins;
edges = h.BinEdges;
x = zeros(1,Nbins);
for counter=1:Nbins
midPointShift = abs(edges(counter)-edges(counter+1))/2;
x(counter) = edges(counter)+midPointShift;
end
mu = mean(data);
sigma = std(data);
f = exp(-(x-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));
两者一起给
hold on;
plot(x,f,'LineWidth',1.5)
在此处输入图片说明
改进很可能归因于实际问题和接受的答案的成功!
编辑-使用hist和histc被不建议现在,和histogram应改为使用。请注意,使用此新功能创建垃圾箱的6种方法均不会产生垃圾箱 hist并histc产生。有一个Matlab脚本可以更新以前的代码以适应 histogram调用方式(bin边而不是bin中心-link)。这样,可以比较pdf @abcd(trapz和sum)和Matlab(pdf)的规范化方法。
3 pdf归一化方法给出的结果几乎相同(在的范围内eps)。
测试:
A = randn(10000,1);
centers = -6:0.5:6;
d = diff(centers)/2;
edges = [centers(1)-d(1), centers(1:end-1)+d, centers(end)+d(end)];
edges(2:end) = edges(2:end)+eps(edges(2:end));
figure;
subplot(2,2,1);
hist(A,centers);
title('HIST not normalized');
subplot(2,2,2);
h = histogram(A,edges);
title('HISTOGRAM not normalized');
subplot(2,2,3)
[counts, centers] = hist(A,centers); %get the count with hist
bar(centers,counts/trapz(centers,counts))
title('HIST with PDF normalization');
subplot(2,2,4)
h = histogram(A,edges,'Normalization','pdf')
title('HISTOGRAM with PDF normalization');
dx = diff(centers(1:2))
normalization_difference_trapz = abs(counts/trapz(centers,counts) - h.Values);
normalization_difference_sum = abs(counts/sum(counts*dx) - h.Values);
max(normalization_difference_trapz)
max(normalization_difference_sum)
在此处输入图片说明
新的PDF规范化与以前的规范化之间的最大差是5.5511e-17。