I have an array of values, said v
, (e.g. v=[1,2,3,4,5,6,7,8,9,10]
) and an array of indexes, say g
(e.g. g=[0,0,0,0,1,1,1,1,2,2]
).
I know, for instance, how to take the first element of each group, in a very numpythonic way, doing:
import numpy as np
v=np.array([1,2,3,4,74,73,72,71,9,10])
g=np.array([0,0,0,0,1,1,1,1,2,2])
mask=np.concatenate(([True],np.diff(g)!=0))
v[mask]
returns:
array([1, 74, 9])
Is there any numpy
thonic way (avoiding explicit loops) to get the maximum of each subset?