6

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]))

2 Answers 2

3

A bit shorter, but maybe I can still do better...

import numpy as np

a = np.arange(12)
b = np.array([5, 9, 2, 4])
b = np.append(b, b[0])

for i in range(0, len(b)-1):
    print np.roll(a, len(a)-b[i])[:b[i+1]-b[i]+1]
Sign up to request clarification or add additional context in comments.

4 Comments

You could make it a little cleaner by using: np.roll(a,-b[i])[:b[i+1]-b[i]+1]
Thanks, this is cleaner. Although it has potentially many more calls to the roll function, which I would assume could slow things down for very large arrays. Can you think of anyway to do this without using roll? Replacing your np.roll(... line with simply a[b[i]:b[i+1]+1] would work for all cases besides that one where the roll-over occurs.
You could append the whole array a to itself, and recalculate the indices for a[start:end], but I think that would be more complicated, not less. I'm sure there is a way to do this with strides, just haven't had the time to try...
I was thinking that it could be done with strides as well. I don't know them well at all though. If I find time, I'll try to investigate them more.
1

Not sure if this helps, but a fast way without messing with the memory of a would be this:

import numpy as np

a = np.arange(12)
b = np.array([5, 9, 2, 4])
b = np.append(b, b[0])

b2 = b.copy()

b2[(np.diff(b)<0).nonzero()[0]+1:] += a.size

print [np.take(a, np.r_[b2[i]:b2[i+1]+1], mode='wrap') for i in range(b.size-1)]

print [np.roll(a, len(a)-b[i])[:b[i+1]-b[i]+1] for i in range(b.size-1)]

%timeit [np.take(a, np.r_[b2[i]:b2[i+1]+1], mode='wrap') for i in range(b.size-1)]
# 10000 loops, best of 3: 28.6 µs per loop

%timeit [np.roll(a, len(a)-b[i])[:b[i+1]-b[i]+1] for i in range(b.size-1)]
# 10000 loops, best of 3: 77.7 µs per loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.