![](https://secure.gravatar.com/avatar/9a86268d4af8e00c5419de66347bf77c.jpg?s=120&d=mm&r=g)
This works fine: In [49]: numpy.zeros((3,2))+numpy.array([1,2]) Out[49]: array([[ 1., 2.], [ 1., 2.], [ 1., 2.]]) but this doesn't: In [50]: numpy.zeros((3,2))+numpy.array([1,2,3]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\Dev\MyProjects\Bio\Python\site_dpm\<ipython console> in <module>() ValueError: shape mismatch: objects cannot be broadcast to a single shape
<ipython console>(1)<module>()
what is the simplest way to get this addition to work? I would like the following array([[ 0., 0.], [ 0., 0.], [ 0., 0.]]) + array([1,2,3]) = array([[ 1., 1.], [ 2., 2.], [ 3., 3.]]) In general my arrays are held in variables and are not constructed on the fly so while this works numpy.zeros((3,2)).T+numpy.array([1,2,3]) which works fine with the '+' operator but not with '+='. This doesn't work: In [54]: a=zeros((3,2)) In [55]: a.T += numpy.array([1,2,3]) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) C:\Dev\MyProjects\Bio\Python\site_dpm\<ipython console> in <module>() AttributeError: attribute 'T' of 'numpy.ndarray' objects is not writable
<ipython console>(1)<module>()
My guess is that I should do a = (a.T + numpy.array([1,2,3])).T but I wonder if there is a more efficient way. Am I missing something? Thanks, John.
![](https://secure.gravatar.com/avatar/ac4e2152839c56c5326cd95ae54296e9.jpg?s=120&d=mm&r=g)
On Tue, Mar 4, 2008 at 6:00 PM, John Reid <j.reid@mail.cryst.bbk.ac.uk> wrote:
This works fine:
In [49]: numpy.zeros((3,2))+numpy.array([1,2]) Out[49]: array([[ 1., 2.], [ 1., 2.], [ 1., 2.]])
but this doesn't: In [50]: numpy.zeros((3,2))+numpy.array([1,2,3]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last)
C:\Dev\MyProjects\Bio\Python\site_dpm\<ipython console> in <module>()
ValueError: shape mismatch: objects cannot be broadcast to a single shape
<ipython console>(1)<module>()
what is the simplest way to get this addition to work? I would like the following
array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])
+
array([1,2,3])
=
array([[ 1., 1.], [ 2., 2.], [ 3., 3.]])
In general my arrays are held in variables and are not constructed on the fly so while this works
numpy.zeros((3,2)).T+numpy.array([1,2,3])
which works fine with the '+' operator but not with '+='. This doesn't work: In [54]: a=zeros((3,2))
In [55]: a.T += numpy.array([1,2,3]) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last)
C:\Dev\MyProjects\Bio\Python\site_dpm\<ipython console> in <module>()
AttributeError: attribute 'T' of 'numpy.ndarray' objects is not writable
<ipython console>(1)<module>()
My guess is that I should do a = (a.T + numpy.array([1,2,3])).T
but I wonder if there is a more efficient way. Am I missing something?
Thanks, John.
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
You could try: aT = a.T aT += numpy.array([1,2,3]) Since aT is a view the data is the same as a, so a itself will be updated.
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Tue, Mar 4, 2008 at 12:00 PM, John Reid <j.reid@mail.cryst.bbk.ac.uk> wrote:
This works fine:
In [49]: numpy.zeros((3,2))+numpy.array([1,2]) Out[49]: array([[ 1., 2.], [ 1., 2.], [ 1., 2.]])
but this doesn't: In [50]: numpy.zeros((3,2))+numpy.array([1,2,3]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last)
C:\Dev\MyProjects\Bio\Python\site_dpm\<ipython console> in <module>()
ValueError: shape mismatch: objects cannot be broadcast to a single shape
<ipython console>(1)<module>()
what is the simplest way to get this addition to work?
Instead of adding a shape-(3,) array, make it a shape-(3,1) array. In [1]: from numpy import * In [2]: z = zeros((3,2)) In [3]: a = array([1,2,3]) In [5]: z + a[:,newaxis] Out[5]: array([[ 1., 1.], [ 2., 2.], [ 3., 3.]]) A good overview of broadcasting is here: http://www.scipy.org/EricsBroadcastingDoc -- 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
participants (3)
-
John Reid
-
Robert Kern
-
Robin