0

I'm aware that dstack can do this:

array([0, 1, 2])
array([3, 4, 5])

to:

array([[[0,3], 
        [1,4], 
        [2,5]]]

But I want this without looping:

array([[[0,3],
        [0,4],
        [0,5],
        [1,3],
        [1,4],
        [1,5],
        [2,3],
        [2,4],
        [2,5]]])

Is it possible? It's like a many to many relationship and I know I can do with pandas, but would there be a simpler and more straight way?

caiohamamura
  • 2,260
  • 21
  • 23

1 Answers1

1

The np.repeat and np.tile methods do what you want.

x = np.array( [0,1,2] )
y = np.array( [3,4,5] )
z = np.dstack( (np.repeat(x,3), np.tile(y,3) ) )
>>> print z
array([[[0, 3],
        [0, 4],
        [0, 5],
        [1, 3],
        [1, 4],
        [1, 5],
        [2, 3],
        [2, 4],
        [2, 5]]])
Gabriel
  • 10,524
  • 1
  • 23
  • 28