0

I have a 2D ndarray as follows:

[[     0      0   1024   1024 572911]
 [     0      0    822   1024 316522]
 [   700      0     95     40   2189]
 [   986     72     38     37    648]
 [   933    158     50     51    823]
 [   720    172     39     85   1806]
 [   863    204    161    255  12329]
 [   407    275     55     34    838]
 [   522    414    234    161  12692]
 [   861    547     49     73   1373]
 [   972    564     52     49   1252]
 [   929    577     25     42    376]
 [   703    608     37     64   1082]
 [   565    612    106    152   6278]
 [   556    615    468    409 113116]
 [   114    674     43     44    478]
 [   155    733    150     53   3505]
 [    57    991     23     30    358]]

and I want to find the index of the n subarrays that maximize the last value in the subarray. In this case, for n=2 the result would be [0, 1]. What means the arrays are [ 0 0 1024 1024 572911] and [ 0 0 822 1024 316522]. How can I achieve that?

Apollo
  • 164
  • 8
  • Does this answer your question? [heapq.nlargest](https://docs.python.org/3.10/library/heapq.html#heapq.nlargest) – Fractalism Feb 12 '23 at 21:00
  • See [`numpy.argpartition`](https://numpy.org/doc/stable/reference/generated/numpy.argpartition.html). Something like `arr[np.argpartition(arr[:,-1], 2)[:2]]` – Jérôme Richard Feb 12 '23 at 21:02

0 Answers0