Note that in matlab’s hist(x, vec)
, vec difines the bin-centers, while in matlab histc(x, vec)
vec defines the bin-edges of the histogram. Numpy’s histogram seems to work with bin-edges. Is this difference important to you? It should be easy to convert from one to the other, and you might have to add an extra Inf
to the end of the bin-edges to get it to return the extra bin you want. More or less like this (untested):
import numpy as np
def my_hist(x, bin_centers):
bin_edges = np.r_[-np.Inf, 0.5 * (bin_centers[:-1] + bin_centers[1:]),
np.Inf]
counts, edges = np.histogram(x, bin_edges)
return counts
For sure it does not cover all the edge-cases that matlab’s hist
provides, but you get the idea.