Shifting numpy array contents

Heiko h.stegmann at arcor.de
Fri Mar 7 16:08:03 CET 2003


John,

I have to admit that my post was really quite imprecise. You'll see
what I mean below - meanwhile, I have found a solution that does what
I want, and it's beautifully simple:

Consider, e.g., 
a = array([[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9],
        [10, 11, 12]])

I want to, for example, shift the 2nd row one cell to the right (with
wrapping around at the ends):

a[1]=concatenate((a[1,-1:],a[1,:-1]))

[[ 1  2  3]
 [ 6  4  5]
 [ 7  8  9]
 [10 11 12]]

now last the row two to the left:

a[3]=concatenate((a[3,2:],a[3,:2]))

[[ 1  2  3]
 [ 6  4  5]
 [ 7  8  9]
 [12 10 11]]

In general, to shift row i by j cells (left for j>0 and right for
j<0):

a[i]=concatenate((a[i,j:],a[i,:j]))




More information about the Python-list mailing list