1

I have two numpy Array with the same SHAPEs. One with values and one with "zones". I need to find max value and index of the value in valuearr which overlap zone 3 in zonearr:

import numpy as np
valuearr = np.array([[10,11,12,13],
                     [21,22,23,24],
                     [31,32,33,34],
                     [41,42,43,44]])

zonearr = np.array([ [0,0,1,1],
                     [0,0,1,1],
                     [3,3,0,0],
                     [3,3,0,0]])

Im trying:

valuearr[np.argwhere(zonearr==3)].max()
44

When it should be 42.

To get index i try

ind = np.unravel_index(np.argmax(valuearr[np.argwhere(zonearr==3)], axis=None), valuearr.shape)

Which of course doesnt work since max value is not 44 and also give error:

builtins.ValueError: index 19 is out of bounds for array with size 16

4
  • 1
    For me print(valuearr[zonearr==3].max()) gives 42. Maybe make sure that your zonearr is of dtype=np.int. Commented Feb 8, 2019 at 10:01
  • @ThomasKühn, zonearr.dtype give int32 Commented Feb 8, 2019 at 10:02
  • Oh, I think I only start to understand now: did you also want to find the correct index of the value 42? Commented Feb 8, 2019 at 10:04
  • Yes that is giving me 42 which is correct. Yes i also want the index of max value Commented Feb 8, 2019 at 10:04

2 Answers 2

2

You can use a masked array to do what you want.

With:

import numpy as np
valuearr = np.array([[10,11,12,13],
                     [21,22,23,24],
                     [31,32,33,34],
                     [41,42,43,44]])

zonearr = np.array([ [0,0,1,1],
                     [0,0,1,1],
                     [3,3,0,0],
                     [3,3,0,0]], dtype=np.int)

First mask out all the values where zonearr is not equal to 3:

masked = np.ma.masked_array(valuearr, mask = (zonearr!=3))

Then find the position of the maximum value with argmax:

idx_1d = np.argmax(masked)

Finally, convert it into a 2d index:

idx_2d = np.unravel_index(idx_1d, valuearr.shape)

and print:

print(idx_2d, valuearr[idx_2d])

which gives:

(3, 1) 42
Sign up to request clarification or add additional context in comments.

1 Comment

@BERA, please check this solution, it works perfectly.
1

Please try the below code

np.max(valuearr[np.where(zonearr==3)])

It fetches the indices of the elements from zonearr, where the value equals to '3'. Followed by, obtaining the maximum element from valuearr through the obtained indices.

To obtain the index of the element 42(as per your example), please use the below code:

np.argwhere(valuearr==np.max(valuearr[np.where(zonearr==3)]))

4 Comments

I don't see any differences in the results. But, just the differences in the approach used.
Simpler way: maxval=valuearr[zonearr==3].max() followed by index=index=np.argwhere(valuearr==maxval)
I Think that might find index of larger max values in other zones than 3. (my example data is not representing the real data)
@BERA, please look at the below solution, i.e, using a masked array, which works perfectly.

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.