在 MATLAB 中,我使用
repmat(A-B,100,1).*rand(100,length(B))
這里A和B是 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)
我該怎么辦?
uj5u.com熱心網友回復:
MATLAB.*是一個廣播運算子。它對運算子進行標量擴展,*以應用于大小匹配的矩陣pointwise。這不是 矩陣乘法,它是在需要中間兩個維度匹配的矩陣上可用的不同操作。
.*在 Python 中,假設左側和右側是numpy陣列,相當于*.
np.matlib.repmat(A - B, 100, 1) * np.matlib.rand(100, len(B))
如果左側和右側不是 numpy陣列(例如,如果它們是普通的 Python 串列),那么您可以通過numpy.array預先呼叫它們來轉換它們
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/462200.html
上一篇:生成二維NumPy陣列的索引
