-1

Let say we have array x

x = np.array([1, 2, 3, 99, 99, 3, 2, 1])
x1, x2, x3 = np.split(x, [3, 6])

I want to use append this two splited array let say x1 and x2 I want to Concatenate like [1, 2, 3, 2, 1] If I am using for Concatenate

x4 = x1+x3
x4

We are getting error like ValueError: operands could not be broadcast together with shapes (3,) (2,)

What we can do for Concatenation two splited array . can you please help me with this

petezurich
  • 9,280
  • 9
  • 43
  • 57

2 Answers2

0

You might use np.hstack to get desired result:

import numpy as np
x = np.array([1, 2, 3, 99, 99, 3, 2, 1])
x1, x2, x3 = np.split(x, [3, 6])
x4 = np.hstack((x1, x3))
print(x4)  # [1 2 3 2 1]

hstack stands for horizontal stack, there also exist vstack for vertical stack, these functions can be also used with multidimensional arrays.

Daweo
  • 31,313
  • 3
  • 12
  • 25
-1

+ is element-wise addition for numpy arrays. I guess you are looking for np.concatenate.

>>> np.concatenate([x1, x2])                                                    
array([ 1,  2,  3, 99, 99])
timgeb
  • 76,762
  • 20
  • 123
  • 145