zhang yunfeng wrote: > Hi, I'm newbie to Numpy. > > When reading tutorials at > http://www.scipy.org/Tentative_NumPy_Tutorial > <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? > I'm not sure what the rules are but this example throws an error, which it should. [Dbg]>>> x= N.array([1, 2, 3]) [Dbg]>>> y= x+[1, 2, 3, 4] Traceback (most recent call last): File "<interactive input>", line 1, in <module> ValueError: shape mismatch: objects cannot be broadcast to a single shape [Dbg]>>> > 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]]) > -------------------------------------------- > This seems a reasonable operation.
Colin W. > -- > http://my.opera.com/zhangyunfeng > > > ------------------------------------------------------------------------ > > _______________________________________________ > Numpy-discussion mailing list > [email protected] > http://projects.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
