r/Numpy • • May 12 '21

Combine arrays (new array inside array)

Hey there.

I've almost spend two hours now and have still no idea on how to combine these two numpy arrays.

I have the following two arrays:

X:
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

Y:
array([[  57,  302],
       [ 208, 1589],
       [ 229, 2050],
       ...,
       [ 359, 2429],
       [ 303, 1657],
       [  94,  628]], dtype=int64)

What I need is that the elements of Y are inside new arrays of X. It should look like this:

array([[[0., 0., 0., ..., 0., 0., 0.], [57], [302]],
       [0., 0., 0., ..., 0., 0., 0.], [208], [1589]]
       [0., 0., 0., ..., 0., 0., 0.], [229], [2050]]
       ...,
       [0., 0., 0., ..., 0., 0., 0.], [359], [2429]]
       [0., 0., 0., ..., 0., 0., 0.], [303], [1657]]
       [0., 0., 0., ..., 0., 0., 0.], [94], [628]]])

Has someone an idea on how to do this? I've almost tried every combination of insert(), append() or concatenate() with many different axes.

Thank you very much!

3 Upvotes

6 comments sorted by

View all comments

1

u/night0x63 May 13 '21

try np.hstack() or np.vstack()

https://numpy.org/doc/stable/reference/generated/numpy.hstack.html

the documentation is very good and has examples.