Home > Software design >  Efficient way to "broadcast" the sum of elements of two 1D arrays to a 2D array
Efficient way to "broadcast" the sum of elements of two 1D arrays to a 2D array

Time:03-04

Is there a more efficient way (without loops) to do this with Numpy ?:

for i, x in enumerate(array1):
    for j, y in enumerate(array2):
        result[i, j] = x   y

I was trying to use einsum without success yet.

Thank you !

CodePudding user response:

Simply use broadcasting with an extra dimension:

result = array1[:,None] array2
  • Related