Quickest way to find the nth largest value in a numpy Matrix

前端 未结 6 1897
孤独总比滥情好
孤独总比滥情好 2020-12-29 06:56

There are lots of solutions to do this for a single array, but what about a matrix, such as:

>>> k
array([[ 35,  48,  63],
       [ 60,  77,  96],
          


        
6条回答
  •  甜味超标
    2020-12-29 07:35

    You can flatten the matrix and then sort it:

    >>> k = np.array([[ 35,  48,  63],
    ...        [ 60,  77,  96],
    ...        [ 91, 112, 135]])
    >>> flat=k.flatten()
    >>> flat.sort()
    >>> flat
    array([ 35,  48,  60,  63,  77,  91,  96, 112, 135])
    >>> flat[-2]
    112
    >>> flat[-3]
    96
    

提交回复
热议问题