[Numpy-discussion] sum of two arrays with different shape?

zhang yunfeng zhangyunfeng at gmail.com
Sun Dec 17 21:26:41 EST 2006


Hi, I'm newbie to Numpy.

When reading tutorials at
http://www.scipy.org/Tentative_NumPy_Tutorial, I found a snippet about
addition of two arrays with different shape, Does it make sense? If
array shapes are not same, why it doesn't throw out an error?

see the code below (taken from the above webpage)
array a.shape is (4,) and y.shape is (3,4)  and a+y ?

-------------------------------------------
>>> y = arange(12)
>>> y
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> y.shape = 3,4              # does not modify the total number of
elements
>>> y
array([[ 0,  1,  2,  3],
      [ 4,  5,  6,  7],
      [ 8,  9, 10, 11]])

It is possible to operate with arrays of diferent dimensions as long as
they fit well.
>>> 3*a                                # multiply each element of a by 3
array([ 30,  60,  90, 120])
>>> a+y                                # sum a to each row of y
array([[10, 21, 32, 43],
      [14, 25, 36, 47],
      [18, 29, 40, 51]])
--------------------------------------------

-- 
http://my.opera.com/zhangyunfeng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20061218/37ee4d8b/attachment.html>


More information about the NumPy-Discussion mailing list