Can numpy do better than this?

Ian Kelly ian.g.kelly at gmail.com
Thu Jan 8 20:36:23 EST 2015


On Thu, Jan 8, 2015 at 6:13 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> With that I came up with the expression
>
> transpose(array([list(roll(mat[:,i],i,0)) for i in range(mat.shape[1])]))
>
> Not exactly pretty.
> My hunch is it can be improved??...

Hmm, you could use the column_stack constructor to avoid having to
transpose the array.

>>> np.column_stack(np.roll(mat[:,i],i,0) for i in range(mat.shape[1]))
array([[ 1, 38, 33, 28, 23, 18],
       [ 7,  2, 39, 34, 29, 24],
       [13,  8,  3, 40, 35, 30],
       [19, 14,  9,  4, 41, 36],
       [25, 20, 15, 10,  5, 42],
       [31, 26, 21, 16, 11,  6],
       [37, 32, 27, 22, 17, 12]])



More information about the Python-list mailing list