1

I'm back with another numpy array question.

I have a 2D array:

A = np.array([[0,0,1,1,0,0,0],[0,1,1,0,1,0,0],[1,0,0,1,1,0,0]])
=
array([[0, 0, 1, 1, 0, 0, 0],
       [0, 1, 1, 0, 1, 0, 0],
       [1, 0, 0, 1, 1, 0, 0]])

and an array of shifts

shifts = np.array([2,4,1])
=
array([2, 4, 1])

Is there a way I can do the equivalent of np.roll(A,shifts) and it will roll A[0,:] by shifts[0], and so forth?

For clarification, I want to do this without for loops, I have already implemented with for loops and now need to optimise the speed (I am in reality working with much larger arrays than those used in the example).

Jack
  • 107
  • 7
  • for clarification: you want to roll each entry along `axis=0` by a value that is given by a list of length `shape[0]`? You could use a simple `for` loop structure... – Cpt.Hook Nov 28 '22 at 15:05
  • Yes that's what I want, sorry I should have specified, I'm specifically trying to avoid for loops. The example I have provided is a tiny version of what I am actually facing with much MUCH larger arrays @Cpt.Hook – Jack Nov 28 '22 at 15:16
  • show the desired result and/or the looping code. – hpaulj Nov 28 '22 at 16:13
  • There was a similar question not too long ago (and probably more). It's possible to construct an advanced indexing array that will do the desired mapping. – hpaulj Nov 28 '22 at 16:17
  • @obchardon I think so. In reality I was oversimplifying because I'm actually working with 3D arrays but I think the answer you provided is the best I will find. – Jack Nov 29 '22 at 07:51

0 Answers0