簡體   English   中英

向量化標量之和乘以矩陣,其中縮放器是列表的元素

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

我正在嘗試對以下內容進行矢量化

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

以上產生了一個 2x2 矩陣,其中所有條目都是 15。如何通過矢量化來實現這一點? 我一直在嘗試將 a 轉換為兩個 2x2 矩陣,但我一無所獲

謝謝

這看起來很簡單:

b * a.sum()

Output:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM