python - Multiply Numpy arrays by distributing terms -


i have 2 one-d numpy arrays in files.

'test1'=(2,3)

'test2'=(5,6,7)

i multiply them get

t=(10, 12, 14, 15, 18, 21)

i using program

import numpy np  a=open('test1') b=open('test2') c=open('test3','w+') t1=np.loadtxt(a) t2=np.loadtxt(b) t=t1*t2  print >> c, t 

when run program, following error..

valueerror: operands not broadcast shapes (2) (3)

what should desired result?

using numpy.outer , numpy.ravel

>>> import numpy np >>> = np.array((2,3)) >>> b = np.array((5,6,7)) >>> np.outer(a,b).ravel() array([10, 12, 14, 15, 18, 21]) 

edit:

subraction: can't use numpy.outer, can use numpy.newaxis:

>>> (a[:, np.newaxis] - b).ravel() array([-3, -4, -5, -2, -3, -4]) 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -