Python Practice › NumPy Programs › Program 39

Add a vector to each row

EasyNumPy Programs · 39 of 100

Write a NumPy program to add a vector to each row of a matrix.

Advertisement
Input
[2, 5] and [[5, 3], [7, 4]]
Output
[[7 8]
 [9 9]]

Approach

  1. Broadcasting adds the vector to every row.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment