Python Practice › NumPy Programs › Program 39
Add a vector to each row
Write a NumPy program to add a vector to each row of a matrix.
Input
[2, 5] and [[5, 3], [7, 4]]
Output
[[7 8] [9 9]]
Approach
- Broadcasting adds the vector to every row.
Solution
import numpy as npprint(np.array([[5, 3], [7, 4]]) + np.array([2, 5]))Output
[[7 8] [9 9]]
Learn the concept
New to this topic? Read the lesson, then come back.