简体   繁体   中英

how to add two numpy.array with indices or mask

I have two arrays, like as following.

 a=numpy.array((1,2,3,4))
 b=numpy.array((1,2,3,4))

I just want to add the first two elements. How can i do it with a mask [True, True, False, False] , or indice [0,1] After addition b=(2, 4,3,4)

It's simply:

b[:2] += a[:2]

Or:

mask = numpy.array((True, True, False, False))
b[mask] += a[mask]

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