0

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?

2
  • Your example and question are not well defined.. do you mean just to add the first column to a 1d array? Commented May 15, 2020 at 22:32
  • Yes, I just want to add first column. Commented May 15, 2020 at 22:34

2 Answers 2

1
import numpy as np

x = np.zeros((3,2))
y = np.array([1,2,3])

x[:, 0] += y
Sign up to request clarification or add additional context in comments.

Comments

0

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]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.