Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve indices of certain value in a matrix and put it in a nested list

I have this matrix; let’s say there are hundreds of x value and thousands of y lists inside this matrix:

[[100   0   0 ...   0   0   0]
 [  0 100   0 ...   0   0   0]
 [  0   0 100 ...   0   0   0]
 ...
 [  0   0   0 ... 100   0   0]
 [  0   0   0 ...   0 100   0]
 [  0   0   0 ...   0   0 100]]

How will I be able to retrieve the index of value >= 90 and put it in a nested list?

Here is the sample output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[0], [1, 167], [2, 498, 2890] ... [6568, 99998], [7894, 19695, 99999], [873, 100000]]

>Solution :

Try using nested list comprehension:

[[idx for idx, value in enumerate(row) if value >= 90] for row in arr]

To have the result of @azro’s, I’d rather use:

np.argwhere(arr >= 90)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading