I want to add 1D numpy array to 2D numpy array?
For example:
Array1: 0 0
0 0
0 0
Array2: 1,2,3
Result: 1 0
2, 0
3, 0
How can I do it in python?
I want to add 1D numpy array to 2D numpy array?
For example:
Array1: 0 0
0 0
0 0
Array2: 1,2,3
Result: 1 0
2, 0
3, 0
How can I do it in python?
We can do this as a vector operation, instead of in a loop:
import numpy as np
array1 = np.array([[0, 0], [0,0],[0,0]])
array2 = np.array([1,2,3])
Note that the first element of the transpose of array1 is the column vector you'd like to add array2 to:
array1.T[0]
Out[10]: array([0, 0, 0])
So we can:
array1.T[0] = array1.T[0] + array2
array1
Out[12]:
array([[1, 0],
[2, 0],
[3, 0]])