python - What is the nicest way to weight planes in a numpy array? -


i have following code in w 1d numpy array of compatible dimension, , m 4d array,

i = 0 weight in w:     m[:, :, i, :] *= weight     += 1 

is there nicer way achieve same effect?

you scaling m along axis=2 elements w, 1d array. so, need extend w 2d array np.newaxis/none, align axes between extended version of w m. then, perform element-wise multiplication between these 2 arrays bring in broadcasting vectorized solution, -

m *= w[:,none] 

if axis=2 of m has length more number of elements in w, need select range along axis=2 in m before multiplying, -

m[...,np.arange(w.size),:] *= w[:,none] 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -