[Numpy-discussion] Can not update a submatrix

Nadav Horesh nadavh@visionsense....
Wed Jan 30 12:42:40 CST 2008


Thank you very much,
that what I was looking for.
Charles made a good point about offsets, counts and strides --- I really should go and reread the documentation.

  Nadav.

-----Original Message-----
From: numpy-discussion-bounces@scipy.org on behalf of lorenzo bolla
Sent: Wed 30-Jan-08 17:31
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Can not update a submatrix
 
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@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@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@scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
>
>
>
> --
> Lorenzo Bolla
> lbolla@gmail.com
> http://lorenzobolla.emurse.com/




-- 
Lorenzo Bolla
lbolla@gmail.com
http://lorenzobolla.emurse.com/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3798 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080130/b3fc721f/attachment.bin 


More information about the Numpy-discussion mailing list