python numpy roll with padding

There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Pad seems to be quite powerful and can do much more than a simple “roll”. The tuple ((0,0),(1,0)) used in this answer indicates the “side” of the matrix which to pad.

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

print np.pad(x,((0,0),(1,0)), mode="constant")[:, :-1]

Giving

[[0 1 2]
 [0 4 5]]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)