Note that this question is not about multiple conditions within a single np.where()
, see this thread for that.
I have a numpy
array arr1
with some numbers (without a particular structure):
arr0 = \
np.array([[0,3,0],
[1,3,2],
[1,2,0]])
and a list of all the entries in this array:
entries = [0,1,2,3]
I also have another array, arr1
:
arr1 = \
np.array([[4,5,6],
[6,2,4],
[3,7,9]])
I would like to perform some function on multiple subsets of elements of arr1
. A subset consts of numbers which are at the same position as arr0
entries with a cetrain value. Let this function be finding the max
value. Performing the function on each subset via a list comprehension:
res = [np.where(arr0==index,arr1,0).max() for index in entries]
res
is [9, 6, 7, 5]
As expected: 0
in arr0
is on the top left, top right, bottom right corner, and the biggest number from the top left, top right, bottom right entries of arr1
(ie 4, 6, 9) is 9. Rest follow with a similar logic.
How can I achieve this without iteration?
My actual arrays are much bigger than these examples.