简体   繁体   English

向量化标量之和乘以矩阵,其中缩放器是列表的元素

[英]Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

I'm trying to vectorise the following我正在尝试对以下内容进行矢量化

a = np.array([1,2])
b = np.array([[5,5],[5,5]])
target = 0
for _ in a:
    target = target + _ * b
    

The above yields a 2x2 matrix where all entries are 15. How can I achieve this through vectorisation?以上产生了一个 2x2 矩阵,其中所有条目都是 15。如何通过矢量化来实现这一点? I've been trying to cast a to be two 2x2 matrices, but I'm not getting anywhere我一直在尝试将 a 转换为两个 2x2 矩阵,但我一无所获

Thanks谢谢

This looks like a simple:这看起来很简单:

b * a.sum()

Output: Output:

array([[15, 15],
       [15, 15]])

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

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