可乐不冰怎么喝 发表于 2022-6-17 14:51:39

求助python两个矩阵元素相乘

import numpy as np #导入numpy库:用于矩阵运算
import numpy.matlib
m = 2
n = 3
d = np.array([, ])
b = np.mat()
theta = np.matlib.repmat(b,1,n)
a = np.matlib.repmat(theta,m,1)

print(d * a)

想问一下大佬们,为啥会出现这个错误:ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)

傻眼貓咪 发表于 2022-6-17 15:42:38

矩阵相乘
(m*n 矩阵)*(n*p 矩阵)
n 必须相同

傻眼貓咪 发表于 2022-6-17 15:50:05

import numpy as np

A = np.array([, ])
B = np.array([, , ])
C = np.dot(A, B)

print(C)[
]

傻眼貓咪 发表于 2022-6-17 15:54:50

import numpy as np

A = np.mat([, ])
B = np.mat([, , ])

print(A * B)[[ 5864]
]

傻眼貓咪 发表于 2022-6-17 16:13:04

你的代码:import numpy as np
import numpy.matlib

m = 2
n = 3

d = np.array([, ])
b = np.mat()

theta = np.matlib.repmat(b, 1, m) # <------ 注意这里
a = np.matlib.repmat(theta, n, 1) # <------ 注意这里

print(d * a)[
[ 8585]]
页: [1]
查看完整版本: 求助python两个矩阵元素相乘