Let's say I have a
numpy.ndarray
shape (2,3,2)
arr = np.array([[[1,3], [2,5], [1,2]],[[3,3], [6,5], [5,2]]])
arr.shape == (2,3)
arr == [[(1,3), (2,5), (1,2)],[(3,3), (6,5), (5,2)]]
arr
tuple
arr = np.array(
[[[1, 4],
[2, 1],
[5, 2]],
[[3, 3],
[6, 5],
[1, 7]]])
print(np.min(arr, axis=0))
>>> [[1,3],
[2,1],
[1,2]]
>>>Should be
[[1,4],
[2,1],
[1,7]]
First, let's find out which pairs to take:
first_eq = arr[0,:,0] == arr[1,:,0]
which_compare = np.where(first_eq, 1, 0)[0]
winner = arr[:,:,which_compare].argmin(axis=0)
Here, first_eq
is True where the first elements match, so we would need to compare the second elements. It's [False, False, False]
in your example. which_compare
then is [0, 0, 0]
(because the first element of each pair is what we will compare). Finally, winner
tells us which of the two pairs to choose along the second axis. It is [0, 0, 1]
.
The last step is to extract the winners:
arr[winner, np.arange(arr.shape[1])]
That is, take the winner (0 or 1) at each point along the second axis.