把一个向量加到矩阵的每一行:
调用numpy库
完成cs231作业1,numpy
使用一重循环
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x) # Create an empty matrix with the same shape as x
# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
y[i, :] = x[i, :] + v
# Now y is the following
# [[ 2 2 4]
#