简体   繁体   中英

How do i add a dimension to a numpy array and copy the dimension from another numpy array

I have a numpy array with the shape (128, 8) I want to add an extra dimension so it has the shape (128, 168, 8) And add the content of a 168 dimension from another array that has the shape (128, 168, 8). I can always permute the positions of the dimensions if I can somehow add it. Is this possible somehow? I have seen the append and concatenation methods but to no luck.

np.expand_dims(smaller_array, axis=1) + bigger_array

Is the correct solution, thanks!

you can also do:

small[:,None,:]+big

Adding None to indexing creates a new dimension of size 1, and adding to another bigger array will broadcast the small's size=1 dimension to bigger arrays corresponding dimension size (here will be 168)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM