简体   繁体   中英

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

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 !

Simply use broadcasting with an extra dimension:

result = array1[:,None]+array2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM