'adding two three dimensional numpy array:

I have two numpy array : X shape is (68,44,13) and X_toadd shape is: (68,44,7)I want to add them together in a way that I will  have X_new shape as (68,44, 20). So, I need to keep the first two dimensions of X and add the 7 columns from X_toadd's third dimension to the 13 columns. how should I do that?

add, append, and concatenate are tried but the result is not what I want which should have the shape (68,44,20)!



Solution 1:[1]

You need to specify which axis to use to glue things together. Here, -1 denotes the first axis from the the back.

import numpy as np
a,b = np.zeros((68,44,13)), np.zeros((68,44,7))

c = np.concatenate([a,b], axis=-1)

c.shape
(68, 44, 20)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 warped