[英]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.