Hi I have a numpy array like
a = np.array([1,1,1,3,3,6,6,6,6,6,6])
I want to convert it to a continuous array like below
b = np.array([0,0,0,1,1,2,2,2,2,2,2])
I have a code for this using for loop
def fun1(a):
b = a.copy()
for i in range(1,a.shape[0]):
if a[i] != a[i-1]:
b[i] = b[i-1]+1
else:
b[i] = b[i-1]
b = b - b.min()
return b
Is there a way to vectorize this using numpy
? I can use numba to make it faster but I was wondering if there is way to do it with just numpy
.