gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:19:59 26 4
gpt4 key购买 nike

[A,I] = histc([0.9828    0.4662    0.5245    0.9334    0.2163],[0.0191    0.2057    0.2820    0.2851    1.0000])

这是带有结果的 MATLAB 代码:

A =

0 1 0 4 0


I =

4 4 4 4 2

我需要的是我。我试过使用 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. ])

我想获取我的数组/矩阵中每个元素进入的容器。

最佳答案

你要找的是numpy.digitize:

Return the indices of the bins to which each value in input array belongs.

>>> 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]

关于python - 我如何在 numpy 中复制这个 matlab 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32765333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com