[Numpy-discussion] overlapping rows

Timothy Hochberg tim.hochberg at ieee.org
Wed Mar 7 17:19:52 EST 2007


On 3/7/07, Matthew Koichi Grimes <mkg at cs.nyu.edu> wrote:
>
> I would like to twiddle with the strides of a matrix such that the rows
> overlap each other. I've gotten this far:
>
> In [1]: import numpy as N
>
> In [2]: mat = N.arange(12).reshape(3,4)
>
> In [3]: mat
> Out[3]:
> array([[ 0,  1,  2,  3],
>        [ 4,  5,  6,  7],
>        [ 8,  9, 10, 11]])
>
> In [4]: mat.strides
> Out[4]: (16, 4)
>
> In [5]: omat = mat[...]
>
> In [6]: omat.strides = (8,4)
>
> In [7]: omat
> Out[7]:
> array([[0, 1, 2, 3],
>        [2, 3, 4, 5],
>        [4, 5, 6, 7]])
>
> So far so good, but I'd like to have "omat"'s first dimension grow to
> size 6 so that all of the data is used. In other words, I'd like for
> omat to look like:
>
> array([[0, 1, 2, 3],
>        [2, 3, 4, 5],
>        [4, 5, 6, 7],
>        [6, 7, 8, 9],
>        [8, 9, 10, 11]])
>
> However, when I try to resize the first dimension to 6 I get the
> following error:
>
> In [8]: omat.shape = (6,4)
>
> ---------------------------------------------------------------------------
> exceptions.ValueError                                Traceback (most
> recent call last)
>
> /home/mkg/<ipython console>
>
> ValueError: total size of new array must be unchanged
>
>
> Is there any workaround to this? It seems like something I should be
> able to do, since I'm only growing omat into memory that's owned
> by omat.base.
>
> Any help would be much appreciated,


I think you're on thin ice here, but:

>>> a = np.arange(12)
>>> b = np.ndarray([6,4], int, a, strides=[8,4])
>>> b
array([[       0,        1,        2,        3],
       [       2,        3,        4,        5],
       [       4,        5,        6,        7],
       [       6,        7,        8,        9],
       [       8,        9,       10,       11],
       [      10,       11,   458762, 19464224]])

(See the docs for ndarray).

Hmm. Perhaps you meant shape [5,4[?

>>> c = np.ndarray([5,4], int, a, strides=[8,4])
>>> c
array([[ 0,  1,  2,  3],
       [ 2,  3,  4,  5],
       [ 4,  5,  6,  7],
       [ 6,  7,  8,  9],
       [ 8,  9, 10, 11]])

That seems more plausible.


-- 

//=][=\\

tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070307/0c47a0c3/attachment.html>


More information about the NumPy-Discussion mailing list