0

I know there is a way to return the index of the maximum element in an array in python: numpy.argmax(). Is there a way to return index of every non-zero element?

For example

array([[ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  1.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       ..., 
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.],
       [ 0.,  1.,  1., ...,  1.,  0.,  0.]], dtype=float32)

to

[[1, 2, ...,6],
[1,2,...6,8],
...
...
]

1 Answer 1

2

Do you want something like this:

x = np.asarray([0, 1, 2, 3, 0, 1])
In [129]: np.nonzero(x)
Out[129]: (array([1, 2, 3, 5]),)

See: [1], [2], [3]

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I cannot believe why I did not get right answer from google.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.