简体   繁体   English

如何在 numpy 中将向量乘以数组/矩阵元素?

[英]How to multiply a vector by an array/matrix element-wise in numpy?

I have a multidimensional array a whose shape is (32,3,5,5) and an array v with a shape of (32,).我有一个形状为 (32,3,5,5) 的多维数组a和一个形状为 (32,) 的数组v How could I multiply (i,3,5,5) with (i,) for each i using numpy other than a for-loop?对于除 for 循环之外的每个 i,我如何使用 numpy 将 (i,3,5,5) 与 (i,) 相乘?

With a and v as the two arrays, few approaches could be suggested -使用av作为两个数组,可以建议几种方法 -

a*v[:,None,None,None]
a*v.reshape(-1, *[1]*3)
(a.T * v).T
np.einsum('i...,i->i...', a, v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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