如何在numpy中复制这个matlab函数?

pow*_*er2 2 python matlab numpy

[A,I] = histc([0.9828    0.4662    0.5245    0.9334    0.2163],[0.0191    0.2057    0.2820    0.2851    1.0000])
Run Code Online (Sandbox Code Playgroud)

这是带有结果的MATLAB代码:

A =

     0     1     0     4     0


I =

     4     4     4     4     2
Run Code Online (Sandbox Code Playgroud)

我需要的是我.我尝试过使用np.histogram,但它给了我这个:

>>> a,b = np.histogram([0.9828 ,   0.4662 ,   0.5245 ,   0.9334 ,   0.2163],[0.0191   , 0.2057   , 0.2820  ,  0.2851  ,  1.0000])
>>> a
array([0, 1, 0, 4])
>>> b
array([ 0.0191,  0.2057,  0.282 ,  0.2851,  1.    ])
Run Code Online (Sandbox Code Playgroud)

我想得到我的数组/矩阵中的每个元素都进入的bin.

The*_*Cat 7

你在寻找的是numpy.digitize:

返回输入数组中每个值所属的bin的索引.

>>> a = np.digitize([0.9828 ,   0.4662 ,   0.5245 ,   0.9334 ,   0.2163],[0.0191   , 0.2057   , 0.2820  ,  0.2851  ,  1.0000])
>>> print(a)
[4 4 4 4 2]
Run Code Online (Sandbox Code Playgroud)

  • @TheBlackCat - 你必须等待15分钟.http://meta.stackexchange.com/questions/50697/time-limit-on-accepting-an-answer - 你在OP发布后12分钟回答了这个问题. (4认同)