本文介绍了将Matlab hist()与Numpy histogram()匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已阅读此和此,以及一些相关的SO问题,例如.仍然找不到解决办法.
I have read this and this, plus some related SO questions like this. Still can not figure out the solution.
我尝试在Matlab中复制hist()函数,但得到的结果尺寸不同,导致内部值不同.我知道bin-center vs bin-edge,我仍然想匹配Matlab结果.
I try to replicate the hist() function in Matlab, I get the result of different dimensions, that causing the values inside to be different. I am aware of bin-center vs bin-edge, I still want to match Matlab results.
Matlab:
a = [1,2,3];
[w,t] = hist(a);
w = [1, 0, 0, 0, 1, 0, 0, 0, 0, 1]
t = [1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9]
length(t) = 10
Python:
a = [1,2,3]
w,t = histogram(a)
w = [1, 0, 0, 0, 0, 1, 0, 0, 0, 1]
t = [1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
len(t) = 11
我当然可以编写自己的函数,但是如果有内置的东西,我会尽量避免重新发明轮子.
I can of course code my own function, but I am trying to avoid wheel re-invention if there is something built-in.
推荐答案
手动计算垃圾箱中心:
>>> t = np.array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0])
>>> t[:-1] + ((t[1:] - t[:-1])/2)
array([ 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9])
,或者甚至更容易使用 np.diff
:
or even easier with np.diff
:
>>> t[:-1] + np.diff(t)/2
array([ 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9])
这篇关于将Matlab hist()与Numpy histogram()匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!