I have two numpy array's a and b of length 53 and 82 respectively. I would like to merge them into a single array because I want to use the 53+82=135 length array say call it c for plotting.
I tried
c = a+b
but I am getting ValueError: shape mismatch: objects cannot be broadcast to a single shape
Is this possible?
You need to use numpy.concatenate instead of array addition
c = numpy.concatenate((a, b))
Implementation
import numpy as np
a = np.arange(53)
b = np.arange(82)
c = np.concatenate((a, b))
Output
c.shape
(135, )
Use numpy.concatenate
:
In [5]: import numpy as np
In [6]: a = np.arange(5)
In [7]: b = np.arange(11)
In [8]: np.concatenate((a, b))
Out[8]: array([ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
For 1-D arrays you can also use numpy.hstack
:
In [9]: np.hstack((a, b))
Out[9]: array([ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.