Reputation: 15
If I have two or more n-dimenisonal arrays, each of which contains also arrays, how con I concatenate all of them index by index?
An example would be:
A = np.array([[[1,2], [3,4]], [[4,5,6], [1]]])
B = np.array([[[10,20], [3,9]], [[7,5], [2]]])
and i would like to get
C = np.array([[[1,2,10,20], [3,4,3,9]], [[4,5,6,7,5], [1,2]]])
Upvotes: 1
Views: 1508
Reputation: 97641
Note that since your inner lists are of different lengths, you've ended up building 2×2 arrays of lists:
>>> A.shape
(2, 2)
But in numpy, +
acts elementwise, and on lists, +
does concatenation. So:
>>> C = A + B
>>> C
array([[[1, 2, 10, 20], [3, 4, 3, 9]],
[[4, 5, 6, 7, 5], [1, 2]]], dtype=object)
Upvotes: 1