Adarsh
Adarsh

Reputation: 109

Element-wise subtraction of two numpy arrays

I have two numpy arrays say 'a' and 'b' having the dimensions (327600,5) and (3,5) respectively. I want to do an element-wise subtraction of the two arrays. How would I go about doing this without a loop?.

When I do a[0] - b I get a (3,5) array. I want to get a (327600,3,5) array as the result after subtraction.

Upvotes: 2

Views: 3391

Answers (1)

user2357112
user2357112

Reputation: 280778

a[:, np.newaxis] - b

You need to take a view of a with an extra length-1 axis in the shape so the axes line up right for broadcasting.

Upvotes: 2

Related Questions