0

Is there a function that does the following?

Given a matrix

0 0 0
1 1 1
0 0 0

And a 1-D array of "shifting values"

0 1 2

The function would shift all the values, column wise, where each shift per column is specified by shifting_values ?

For example

> np.f(X, shifting_values,axis=1)
0 0 1
1 0 0
0 1 0

Does such a function exist? If not, is it possible to do this without using a for loop?

truvaking
  • 347
  • 2
  • 10
  • You should be able to adapt the answers without problems. For the first one for instance just change `r[r < 0] += A.shape[0]` and transpose the result instead to adapt it to rolling along axis 1 – yatu Apr 27 '20 at 13:33
  • 1
    that worked. thanks, posting the modified version in case it's hard to work it out – truvaking Apr 27 '20 at 13:42
  • ```import numpy as np A = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0]]) r = np.array([2, 0, -1]) rows, cols = np.ogrid[:A.shape[0], :A.shape[1]] # Changes here r[r < 0] += A.shape[0] # Changes here rows = rows - r[np.newaxis, :] # Changes here result = A[rows, cols] ``` – truvaking Apr 27 '20 at 13:43
  • is there an easy way to adapt this to tensors? i'm trying to shift every matrix inside a tensor by a certain amount column wise – truvaking Apr 27 '20 at 18:38

0 Answers0