获取已排序矩阵的索引 [英] Get an index of a sorted matrix

查看:98
本文介绍了获取已排序矩阵的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D np.array:

I have a 2D np.array:

array([[ 1523.,   172.,  1613.],
       [ 3216.,   117.,  1999.],
       [   85.,  1271.,     4.]])

我想按值提取这个np.array的排序索引。

结果应该是(例如):
[[2,2],[2,0],[1,1],[0,1],[2,1],[0,0],[0,2],[1,2],[ 1,0]]

I would to extract the sorted indexes of this np.array by value.
The results should be (for example) : [[2,2],[2,0],[1,1],[0,1],[2,1],[0,0],[0,2],[1,2],[1,0]]

我已经看到了如何提取分钟:

I already saw how to extract the min :

np.unravel_index(np.argmin(act),act.shape) #(2,2)

谢谢

推荐答案

使用 numpy.argsort axis = None (假设展平数组):

Using numpy.argsort with axis=None (assuming flatten array):

>>> import numpy as np
>>> 
>>> act = np.array([[ 1523.,   172.,  1613.],
...                 [ 3216.,   117.,  1999.],
...                 [   85.,  1271.,     4.]])
>>> n = act.shape[1]
>>> zip(*np.argsort(act, axis=None).__divmod__(n))
[(2, 2), (2, 0), (1, 1), (0, 1), (2, 1), (0, 0), (0, 2), (1, 2), (1, 0)]

这篇关于获取已排序矩阵的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆