Get original indices of a sorted Numpy array

后端 未结 2 1937
鱼传尺愫
鱼传尺愫 2020-12-08 09:30

I have an array of distances a = np.array([20.5 ,5.3 ,60.7 ,3.0 ], \'double\') and I need the indices of the sorted array (for example [3, 1, 0, 2]

2条回答
  •  鱼传尺愫
    2020-12-08 10:19

    Here's an example, for reference and convenience:

    # create an array
    a = np.array([5,2,3])
    
    # np.sort - returns the array, sorted
    np.sort(a)
    >>> array([2, 3, 5])
    
    # argsort - returns the original indexes of the sorted array
    np.argsort(a)
    >>> array([1, 2, 0])
    

提交回复
热议问题