jm96
jm96

Reputation: 57

Concatenate two arrays of different dimensions numpy

I have two numpy arrays of the form np.array([1,2,3])) I want to concatenate them so I get:

[[1,2,3],[4,5,6]] 

and it also has to work when there is a numpy array of the form [[1,2,3],[4,5,6]] and another one [7,8,9] and I want to get:

[[1,2,3],[4,5,6],[7,8,9]]

I tried np.concatenate but I couldn't get it to work

Upvotes: 1

Views: 1240

Answers (1)

tom10
tom10

Reputation: 69242

You can use vstack for this:

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.array([7,8,9])

d = np.vstack((a,b))

e = np.vstack((d, c))

print(d)
print(e)

Gives:

[[1 2 3]
 [4 5 6]]    

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Upvotes: 2

Related Questions