python - Scale rows of 3D-tensor -
i have n
-by-3
-by-3
numpy array a
, n
-by-3
numpy array b
. i'd multiply every row of every 1 of n
3
-by-3
matrices corresponding scalar in b
, i.e.,
import numpy np = np.random.rand(10, 3, 3) b = np.random.rand(10, 3) a, b in zip(a, b): = (a.t * b).t print(a)
can done without loop well?
you can use numpy broadcasting
let elementwise multiplication happen in vectorized manner after extending b
3d
after adding singleton dimension @ end np.newaxis
or alias/shorthand none
. thus, implementation a*b[:,:,none]
or a*b[...,none]
.
Comments
Post a Comment