Why does numpy.histogram (Python) leave off one element as compared to hist in Matlab?

后端 未结 2 1872
Happy的楠姐
Happy的楠姐 2021-01-14 01:42

I am trying to convert some Matlab code to Python, and the Matlab code looks like:

[N,X] = hist(Isb*1e6, -3:0.01:0)

where Isb is a 2048000

2条回答
  •  情深已故
    2021-01-14 02:02

    As said above, in matlab -3:0.01:0 specifies the bin-centers, namely 301. What your doing in numpy specifies bin-edges, so this will be one bin less than in matlab.

    Hence, you could either move from hist to histc within matlab, or make sure you apply the same bin-edges in numpy. In this special case (equidistant bins) you could also use numpy like this:

    N,X = np.histogram(x, bins = n_bins, range = (xmin, xmax))
    

    In this case: n_bins of 301 and (xmin, xmax) being (-3.005,0.005) should be equivalent to matlab's hist.

    See also:

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

提交回复
热议问题