Skip to content Skip to sidebar Skip to footer

How To Translate / Shift A Numpy Array?

I am not sure what key-word to search for so if it has been already asked please link the response and close this thread. I am trying to shift the non-zero entries of a numpy array

Solution 1:

Using roll method from numpy.

>>>import numpy as np>>>m
array([[0, 1, 2, 0],
       [0, 3, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
>>>m = np.roll(m, 1, axis=0) # shift 1 place in horizontal axis>>>m = np.roll(m, 1, axis=1) # shift 1 place in vertical axis>>>m
array([[0, 0, 0, 0],
       [0, 0, 1, 2],
       [0, 0, 3, 0],
       [0, 0, 0, 0]])

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.roll.html

Solution 2:

To simply manage the edges, you can enlarge your array in a bigger one :

square=\
array([[0, 2, 2, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int64)

n,m=square.shape
bigsquare=np.zeros((3*n,3*m),square.dtype) 
bigsquare[n:2*n,m:2*m]=square

Then shift is just a view :

def shift(dx,dy):
    x=n-dx
    y=m-dy
    return bigsquare[x:x+n,y:y+m]

print(shift(1,1))

#[[0 0 0 0]# [0 0 2 2]# [0 0 2 0]# [0 0 0 0]]

Post a Comment for "How To Translate / Shift A Numpy Array?"