簡體   English   中英

Python 逐元素乘法

[英]Python Element-wise Multiplication

在 MATLAB 中,我使用

repmat(A-B,100,1).*rand(100,length(B))

這里, AB是1*19大小的矩陣。

要在 Python 中執行相同的代碼,我使用以下代碼 -

np.matmul(np.matlib.repmat(A - B, 100, 1), np.matlib.rand(100, len(B)))

運行代碼后,出現以下錯誤-

ValueError: matmul: 
  Input operand 1 has a mismatch in its core dimension 0, 
  with gufunc signature (n?,k),(k,m?)->(n?,m?) 
  (size 100 is different from 19)

我應該怎么辦?

MATLAB 的.*廣播運算符 它標量擴展*運算符以應用於匹配大小的矩陣pointwise 不是矩陣乘法,它是對需要中間兩個維度匹配的矩陣可用的不同運算。

假設左側和右側是numpy arrays,相當於 Python 中的.* * 就是*

np.matlib.repmat(A - B, 100, 1) * np.matlib.rand(100, len(B))

如果左側和右側不是numpy arrays(例如,如果它們是普通的 Python 列表),則可以通過預先調用numpy.array對其進行轉換

暫無
暫無

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

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