[英]About matrix vector operations in Python
我有一个大小为 nxm 的矩阵和一个大小为 nx1 的向量。 例如:
import numpy as np
matrix = np.array([[1, 2, 3],[4,5,6]])
vector = np.array([[10],[20]])
我想获得result = [[1+10, 2+10, 3+10],[4+20,5+20,6+20]] = [[11, 12, 13],[24,25,26]]
我对 python 中的矩阵运算有点困惑。 我找不到正确的人来做到这一点。
我相信您可以添加它们numpy.add
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
vector = np.array([[10], [20]])
print(vector + matrix)
Output:
[[11 12 13]
[24 25 26]]
添加后,这将为您提供 output。
matrix = [[1, 2, 3],[4,5,6]]
vector = [10,20]
result = []
x = 0
for elem in matrix:
for nums in elem:
result.append(nums+vector[x])
x += 1
print(result)
Output
[11, 12, 13, 24, 25, 26]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.