I have two 1D arrays, one that has some values of interest (a) and another that provides indices into that array (b). I know that the values in b always increase, except at one point (could be anywhere) where the number decreases since it rolls from the end to the beginning of array a. The method below seems to work, but I just think that a cleaner way must exist. Can anyone suggest something better? Thanks.
Code:
import numpy as np
a = np.arange(12)
b = np.array([5, 9, 2, 4])
#I want to generate these:
#[5,6,7,8,9]
#[9,10,11,0,1,2]
#[2,3,4]
#[4,5]
a = np.roll(a, -b[0], axis=0)
# Subtract off b[0] but ensure that all values are positive
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]))