# [Numpy-discussion] ndarray.resize that preserve view content ?

Zachary Pincus zachary.pincus@yale....
Wed Aug 4 10:52:28 CDT 2010

```> Yes it is, but is there a way to do it in-place?

So you want the first 25 elements of the array (in a flat "contiguous"
view) to contain the 25 elements of A[:5,:5]? This will do that, but
having to do stuff like this (rather than just copying the memory
region) might be indicative that maybe your code design might not
really be right. (Why does it absolutely have to be in-place? Memory
pressure?)

In [34]: a = numpy.array(numpy.arange(100, dtype=int).reshape(10,10))

In [35]: a
Out[35]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

In [36]: a[:5,:5]
Out[36]:
array([[ 0,  1,  2,  3,  4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]])

In [37]: a.flat[:25] = a[:5,:5]

In [38]: a.resize((5,5))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call
last)

/Users/zpincus/<ipython console> in <module>()

ValueError: cannot resize an array that has been referenced or is
referencing
another array in this way.  Use the resize function

In [39]: b = numpy.ndarray(buffer=a, shape=(5,5), dtype=int)
In [40]: b

Out[40]:
array([[ 0,  1,  2,  3,  4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]])

In [41]: b.flags.c_contiguous
Out[41]: True

In [42]: b.flags.owndata
Out[42]: False

Zach

> On Wed, Aug 4, 2010 at 5:20 PM, Zachary Pincus <zachary.pincus@yale.edu
> > wrote:
> > A[:5,:5] shows the data I want, but it's not contiguous in memory.
> > A.resize(5,5) is contiguous, but do not contains the data I want.
> >
> > How to get both efficiently?
>
> A[:5,:5].copy()
> will give you a new, contiguous array that has the same contents as
> A[5:,5:], but in a new chunk of memory. Is this what you need?
>
>
>
>
> On Aug 4, 2010, at 11:17 AM, Antoine Dechaume wrote:
>
> > I forgot to refer to resize, sorry about that.
> >
> > A[:5,:5] shows the data I want, but it's not contiguous in memory.
> > A.resize(5,5) is contiguous, but do not contains the data I want.
> >
> > How to get both efficiently?
> >
> >
> > On Wed, Aug 4, 2010 at 5:01 PM, Robert Kern <robert.kern@gmail.com>
> > wrote:
> > On Wed, Aug 4, 2010 at 09:29, Antoine Dechaume <boolegue@gmail.com>
> > wrote:
> > > Hi,
> > >
> > > given A=empty([10,10]), I would like to keep A[:5,:5] as a
> > contiguous memory
> > > segment.
> > >
> > > How to do it efficiently?
> >
> > I'm not sure I understand what you want. Your Subject line and the
> > body of your email conflict with each other. Can you try to explain
> > what you want in other words?
> >
> > --
> > Robert Kern
> >
> > "I have come to believe that the whole world is an enigma, a
> harmless
> > enigma that is made terrible by our own mad attempt to interpret
> it as
> > though it had an underlying truth."
> >   -- Umberto Eco
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

```