Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96378

Concatenating with empty numpy array

I am running into errors when concatenating arrays in Python:

x = np.array([])
while condition:
    % some processing 
    x = np.concatenate([x + new_x])

The error I get is:

----> 1 x = np.concatenate([x + new_x])

ValueError: operands could not be broadcast together with shapes (0) (6) 

On a side note, is this an efficient way to grow a numpy array in Python?

Upvotes: 2

Views: 7745

Answers (2)

daedalus12
daedalus12

Reputation: 394

Alternatively:

x = np.append(x,new_x)

Regarding your side note, take a look here: How to extend an array in-place in Numpy?

Upvotes: 0

Xymostech
Xymostech

Reputation: 9850

Looks like you want to call

x = np.concatenate((x, new_x))

according to the docs.

Upvotes: 3

Related Questions