简体   繁体   中英

Easy way to do this in numpy?

Suppose I have a BxNxL array, M. In other words, M is composed of B NxL matrices. In addition, I have a LxB column vector, Q. Is there any easy way (without for loops) to broadcast (sum) the ith column of Q to the ith matrix in M?

So your iterative code would be?

for i in range(...):    
    res[i,:,:] = M[i,:,:] + Q[:,i]    # NxL + L

with the whole array

res = M + Q.T[:,None,:]     # BxNxL  + (Bx1xL)

(I wrote this without a test example, so there might an error, but the basic idea should be right)

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