On Wed, Jul 27, 2011 at 5:36 PM, Alex Flint <alex.fl...@gmail.com> wrote: > When applying two different slicing operations in succession (e.g. select a > sub-range, then select using a binary mask) it seems that numpy arrays can > be inconsistent with respect to assignment: > For example, in this case an array is modified: > In [6]: A = np.arange(5) > In [8]: A[:][A>2] = 0 > In [10]: A > Out[10]: array([0, 1, 2, 0, 0]) > Whereas here the original array remains unchanged > In [11]: A = np.arange(5) > In [12]: A[[0,1,2,3,4]][A>2] = 0 > In [13]: A > Out[13]: array([0, 1, 2, 3, 4]) > This arose in a less contrived situation in which I was trying to copy a > small image into a large image, modulo a mask on the small image. > Is this meant to be like this? > Alex > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > >
When you do this: A[[0,1,2,3,4]][A>2] = 0 what is happening is: A.__getitem__([0,1,2,3,4]).__setitem__(A > 2, 0) Whenever you do getitem with "fancy" indexing (i.e. A[[0,1,2,3,4]]), it produces a new object. In the first case, slicing A[:] produces a view on the same data. - Wes _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion