[Numpy-discussion] fiddling the strides of an array

Filip Wasilewski filip.wasilewski at gmail.com
Sun Nov 19 15:07:27 CST 2006


On 11/19/06, A. M. Archibald <peridot.faceted at gmail.com> wrote:
> Hi,
>
> I have a function that would like to be able to take an array, look at
> its 'strides' and 'shape' tuples, and fabricate another array that is
> similar to the first but has the adjusted values.
>
> For a simple example:
>
> def fiddle(a):
>     strides = list(a.strides)
>     strides[0]*=2
>     shape = list(a.shape)
>     shape[0]//=2
>     return N.ndarray.__new__(N.ndarray, strides=strides, shape=shape,
> buffer=a, dtype=a.dtype)

Is there anything wrong in using slicing for that?

>>> m=numpy.matrix([[2,3,4], [3,2,1]])
>>> m.shape, m.strides
((2, 3), (12, 4))
>>> n = m[::2,:]
>>> n.shape, n.strides
((1, 3), (24, 4))

cheers,
fw


More information about the Numpy-discussion mailing list