[Numpy-discussion] Can not update a submatrix

lorenzo bolla lbolla at gmail.com
Wed Jan 30 10:31:36 EST 2008


or you can maybe use numpy.ix_:
ax = [1,2]
R[numpy.ix_(ax,ax)] = 100

hth,
L.


On 1/30/08, lorenzo bolla <lbolla at gmail.com> wrote:
>
> you simply need to change the definition of ax:
> ax = slice(1,3)
>
> and all works fine.
> L.
>
>  On 1/30/08, Francesc Altet <faltet at carabos.com> 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,
> >
> > --
> > >0,0<   Francesc Altet     http://www.carabos.com/
> > V   V   Cárabos Coop. V.   Enjoy Data
> > "-"
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
>
>
>
> --
> Lorenzo Bolla
> lbolla at gmail.com
> http://lorenzobolla.emurse.com/




-- 
Lorenzo Bolla
lbolla at gmail.com
http://lorenzobolla.emurse.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080130/cb61aef8/attachment.html>


More information about the NumPy-Discussion mailing list