chris
chris

Reputation: 4996

Numpy built in elementwise append

I have an array 3D array of points onto which I would like to append a corresponding value in a flat array.

points = [[1,2,3],[4,5,6]]
info = [1,2]

Is there a built in way to append elements from the second array to the corresponding positions in the first?

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

Upvotes: 0

Views: 40

Answers (1)

nneonneo
nneonneo

Reputation: 179422

Use np.hstack:

points = np.array([[1,2,3],[4,5,6]])
info = np.array([1, 2])
output = np.hstack([points, info.reshape(2,1)])

Output:

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

Upvotes: 2

Related Questions