Skip to main content
  1. About
  2. For Teams
Asked
Viewed 91 times
1

I'm sorry, I think this is a really trivial question, but...

I have a binary Numpy array, mostly zeros with a few ones. I would like to find the coordinates of all the locations where myArray[myArray == 1]

Please can you help me? Thanks

2
  • 1
    "Array"? List of lists? Numpy array?
    Denziloe
    –  Denziloe
    2017-03-13 16:26:40 +00:00
    Commented Mar 13, 2017 at 16:26
  • 2
    I'm not sure how you're creating myArray, but this could work: [i for i,v in enumerate(myArray) if v == 1].
    gen_Eric
    –  gen_Eric
    2017-03-13 16:27:11 +00:00
    Commented Mar 13, 2017 at 16:27

2 Answers 2

2
np.where(myArray==1)

I guess, should work (assuming its numpy array, based on your indexing example)

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

Comments

0

If you are using a list (rather than a numpy array), this answer uses enumerate in both a loop and list comprehension: https://stackoverflow.com/a/17202481/1160876

Comments

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.