python numpy roll with padding

Posted by Marshall Ward on Stack Overflow See other posts from Stack Overflow or by Marshall Ward
Published on 2010-05-06T01:39:34Z Indexed on 2010/05/06 2:38 UTC
Read the original article Hit count: 1107

Filed under:
|
|

I'd like to roll a 2D numpy in python, except that I'd like pad the ends with zeros rather than roll the data as if its periodic.

Specifically, the following code

import numpy as np

x = np.array([[1, 2, 3],[4, 5, 6]])

np.roll(x,1,axis=1)

returns

array([[3, 1, 2],[6, 4, 5]])

but what I would prefer is

array([[0, 1, 2], [0, 4, 5]])

I could do this with a few awkward touchups, but I'm hoping that there's a way to do it with fast built-in commands.

Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy