【发布时间】:2020-05-03 12:48:01
【问题描述】:
我在 Python 中有 2 个矩阵:形状为 (4,1) 的矩阵 A 和形状为 (4,4) 的矩阵 B。 我使用列表中的数据形成了 2 个矩阵。
valList 数据看起来像
00200030
00200030
00200030
00200030
00480051
FFF0004B
FFF0004B
我将每个项目转换为一个 32 位整数,然后使用这些数据形成矩阵。
for item in valList:
int(item,32)
B_RC = createMatrix(rows,1,valList)
B = np.array(B_RC)
print B
A_RC = valList[rows:rows + (rows * cols)]
A = np.array(A_RC).reshape( (rows,cols))
print A
def createMatrix(rowCount, colCount, dataList):
mat = []
for i in range (rowCount):
rowList = []
for j in range (colCount):
if dataList[j] not in mat:
rowList.append(dataList[i])
mat.append(rowList)
return mat
我想将这两个矩阵相乘。 我使用了 numpy,但下面的代码出现以下错误:
>>> C=np.matmul(B,A)
error: ufunc 'matmul' did not contain a loop with signature matching types dtype('S8') dtype('S8') dtype('S8')
我应该使用什么功能?
【问题讨论】:
-
我认为问题出在您的
A和B矩阵的数据类型上。您能否在问题中发布它们的值? -
我假设
dataList包含所有数字,那么您需要在运行C之前更改 numpy 数组的数据类型,例如A = A.astype("float64")和B = B.astype("float64") -
我试过这个......它让我无法将字符串转换为浮点数:我在列表中的数据项是例如 FFBCFFA2
-
那么,您愿意如何对字符串执行点积?
-
如何将字符串列表转换为 32 位十六进制数?
标签: python numpy matrix matrix-multiplication