如何从Matlab中的旧hist和新histcounts函数获得相同的结果

时间:2016-07-08 09:57:27

标签: matlab histogram binning

我正在尝试用新的histcounts替换旧的hist函数的使用,它在bin和计数方面比hist更快。但是,我很难从hist得到的histcounts中获得完全相同的结果。

我知道histcounts返回bin边缘而不是binCenters。但是,据我所知,计数应该是相同的,并且bin边缘应该可以转换为bin中心。

使用histcounts替换hist的Matlab参考页面(参见http://se.mathworks.com/help/matlab/creating_plots/replace-discouraged-instances-of-hist-and-histc.html)没有触及结果的这种不等式,只将binCenters转换为binEdges(不是相反)

这段小代码有助于说明我的问题。

A = randn(100,2);

[N1,binCenters1] = hist(A(:,1),10);
[N2,binEdges2] = histcounts(A(:,1),10);

binCenters2 = mean([binEdges2(1:end-1);binEdges2(2:end)]);

N = [N1;N2;abs(N1-N2)];
binCenters = [binCenters1;binCenters2;abs(binCenters1-binCenters2)];

输出

N:

1   2   4   7   28  28  16  8   4   2
1   0   5   6   23  34  15  10  4   2
0   2   1   1   5   6   1   2   0   0

binCenters:

-2,46697043006437   -1,93055060769862   -1,39413078533287   -0,857710962967123  -0,321291140601375  0,215128681764373   0,751548504130121   1,28796832649587    1,82438814886162    2,36080797122737
-2,71500000000000   -2,14500000000000   -1,57500000000000   -1,00500000000000   -0,435000000000000  0,135000000000000   0,705000000000000   1,27500000000000    1,84500000000000    2,41500000000000
0,248029569935633   0,214449392301381   0,180869214667129   0,147289037032877   0,113708859398625   0,0801286817643727  0,0465485041301204  0,0129683264958687  0,0206118511383833  0,0541920287726354

如您所见,差异是非零的。

0 个答案:

没有答案