Let's assume I have this array:
prova = np.array([[[0, 8],
[8, 8],
[7, 7]],
[[6, 5],
[6, 6],
[0, 3]],
[[0, 1],
[5, 6],
[0, 2]],
[[6, 2],
[2, 8],
[4, 4]]])
Let's also assume I have a random function that take in input 2 numpy array (not important of what the function does, it is just a random example) and always returns a scalar (int
in this case):
def random_function(a1, a2):
a1 = a1[np.argsort(a1)]
a2 = a2[np.argsort(a2)]
if a1[0]>a2[0]:
print("1")
return np.diff(a2-a1).max()
elif a2[0]>a1[0]:
print("2")
return np.diff(a2-a1).min()
else:
print("3")
return (np.cumsum(a1)+np.cumsum(a2))[-1]
What I want to do is to apply this function on the first axis of prova
in this way:
np.array([random_function(*p.T) for p in prova])
out:
array([-6, -4, -1, 26])
How can I obtain the same result without using a for
loop? Can I use np.apply_along_axis
or np.apply_over_axes
to achieve this goal?
Thank you in advance.