On 5/25/11 3:27 PM, Catherine Moroney wrote:
Hello,

I am trying to work with a structured array and a mask, and am encountering some
problems.

You will want to ask numpy questions on the numpy mailing list:

  http://www.scipy.org/Mailing_Lists

For example:

 >>> xtype = numpy.dtype([("n", numpy.int32), ("x", numpy.float32)])
 >>> a = numpy.zeros((4), dtype=xtype)
 >>> b = numpy.arange(0,4)
 >>> a2 = numpy.zeros((4), dtype=xtype)
 >>> mask = numpy.where(b%2 == 0)
 >>> a2[:]["n"] += b ! this changes the values of a2
 >>> a[mask]["n"] += b[mask] ! this does not change the values of a
 >>> a2
array([(0, 0.0), (1, 0.0), (2, 0.0), (3, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])
 >>> a
array([(0, 0.0), (0, 0.0), (0, 0.0), (0, 0.0)],
dtype=[('n', '<i4'), ('x', '<f4')])

Why do the values of a2 get updated, and yet the values of a do not?

Only the final [] on the left-hand side of the assignment actually turns into a .__setitem__() call to the object that is the result of the expression to its left. a[mask] makes a copy while a2[:] makes a view.

You could do

  a["n"][mask] += b[mask]

since a["n"] will also make a view and the .__setitem__() on it will propagate back to the original memory.

--
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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to