2

I have seen this question, but want to reduce the array created from mask = array == value

mask = array([[[ True,  True,  True],
               [False,  True,  True]],

              [[False,  True,  True],
               [False,  True,  True]],

              [[False, False,  True],
               [False,  True,  True]]])

which results in

where(mask) = (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2]),
               array([0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]),
               array([0, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2]))

and I want to reduce it to an array of the first occurrences of True

array([[0, 1],
       [1, 1],
       [2, 1]])

but can't work out how to go about this from the output of numpy.where. Can anyone help me out?

2
  • 1
    If your array has 3 dimensions, shouldn't the first occurrence of True be array([[0],[0],[0]])? What does "an array of the first occurrences" mean? Commented Mar 6, 2014 at 23:05
  • One solution could be with np.argmax possibly with an axis defined. Commented Mar 6, 2014 at 23:17

1 Answer 1

2

Actually, it's as simple as this:

np.argmax(mask, 2)

Example:

In [15]: %paste
mask = array([[[ True,  True,  True],
               [False,  True,  True]],

              [[False,  True,  True],
               [False,  True,  True]],

              [[False, False,  True],
               [False,  True,  True]]])

## -- End pasted text --

In [16]: np.argmax(mask, 2)
Out[16]:
array([[0, 1],
       [1, 1],
       [2, 1]], dtype=int64)
Sign up to request clarification or add additional context in comments.

Comments

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.