14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

numpyで配列の要素とインデックスを降順に取り出す

Posted at

numpyで配列の要素とインデックスを降順に取り出すには、sort関数とargsort関数を使います。

ただ、sort関数は昇順にソートし、argsort関数は昇順にソートしたインデックスの配列を返すので、[::-1]というスライスの書き方を使って逆順、つまり、降順にします。

import numpy as np

x = np.array([18, 7, 55, 31])

for i in range(len(x)):
    print np.argsort(x)[::-1][i], np.sort(x)[::-1][i]

次のように出力されます。

2 55
3 31
0 18
1 7
14
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?