I have two 1D arrays, one of which has some interest values (a) and the other that provides indexes to this array (b). I know that the values in b always increase, with the exception of one point (maybe anywhere), where the number decreases since it rolls from the end to the beginning of array a. The method shown below works, but I just think there should be a cleaner way. Can anyone suggest something better? Thank.
the code:
import numpy as np
a = np.arange(12)
b = np.array([5, 9, 2, 4])
a = np.roll(a, -b[0], axis=0)
b = (b-b[0]+len(a))%len(a)
for i, ind in enumerate(b):
if i < len(b)-1:
print a[b[i]:b[i+1]+1]
else:
print np.hstack((a[b[i]:len(a)], a[0]))
source
share