[Numpy-discussion] view 1d array as overlapping segments?
Pauli Virtanen
pav@iki...
Mon Mar 7 09:13:30 CST 2011
Mon, 07 Mar 2011 10:01:21 -0500, Neal Becker wrote:
> reshape can view a 1d array as non-overlapping segments.
>
> Is there a convenient way to view a 1d array as a 2d array of
> overlapping segments?
>
> nonoverlapping:
> l: segment length
> k: overlap
> u is the 1d array
> v is a 2d array
>
> v[i] = u[l*i:(l+1)*i]
>
> overlapping:
> v[i] = u[l*i:(l+1)*i+k]
Yes, specify
from numpy.lib.stride_tricks import as_strided
strides = (u.dtype.itemsize*l, u.dtype.itemsize)
shape = (num_slices, l+k)
v = as_strided(u, strides=strides, shape=shape)
num_slices needs to be chosen so that you don't access
out-of-bounds memory. `as_strided` doesn't check for this.
More information about the NumPy-Discussion
mailing list