[Numpy-discussion] fiddling the strides of an array

A. M. Archibald peridot.faceted at gmail.com
Sun Nov 19 16:10:44 EST 2006


On 19/11/06, Filip Wasilewski <filip.wasilewski at gmail.com> wrote:
> 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))

Well, no, of course not to implement "fiddle". But the application I
have in mind I don't think can be done with slicing...

A. M. Archibald



More information about the NumPy-Discussion mailing list