[Numpy-discussion] Can not update a submatrix
Nadav Horesh
nadavh@visionsense....
Wed Jan 30 09:21:40 CST 2008
But:
>>> R[ax,:] = 100
>>> R
array([[ 0, 1, 2],
[100, 100, 100],
[100, 100, 100]])
>>> R[:,ax] = 200
>>> R
array([[ 0, 200, 200],
[100, 200, 200],
[100, 200, 200]])
Do I get an array view only if the array is contiguous?
Nadav.
On Wed, 2008-01-30 at 16:08 +0100, Francesc Altet wrote:
> A Wednesday 30 January 2008, Nadav Horesh escrigué:
> > In the following piece of code:
> > >>> import numpy as N
> > >>> R = N.arange(9).reshape(3,3)
> > >>> ax = [1,2]
> > >>> R
> >
> > array([[0, 1, 2],
> > [3, 4, 5],
> > [6, 7, 8]])
> >
> > >>> R[ax,:][:,ax] = 100
> > >>> R
> >
> > array([[0, 1, 2],
> > [3, 4, 5],
> > [6, 7, 8]])
> >
> > Why R is not updated?
>
> Because R[ax] is not a view of R, but another copy of the original
> object (fancy indexing does return references to different objects).
> In order to get views, you must specify only a slice of the original
> array. For example:
>
> In [50]: S = R[::2]
> In [51]: S[:] = 2
> In [52]: R
> Out[52]:
> array([[2, 2, 2],
> [3, 4, 5],
> [2, 2, 2]])
>
> So, what you need is something like:
>
> In [68]: R = N.arange(9).reshape(3,3)
> In [69]: S = R[1:3,:][:,1:3]
> In [70]: S[:] = 2
> In [71]: R
> Out[71]:
> array([[0, 1, 2],
> [3, 2, 2],
> [6, 2, 2]])
>
> Cheers,
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080130/29a0a723/attachment.html
More information about the Numpy-discussion
mailing list