Reputation: 20895
I am getting an error in numpy when I perform the pairwise multiplication of two arrays a and b since a has dimensions 100 x 200 x 3, while b has dimensions 100 x 200. However, b contains only 0s and 1s. How do I repeat the last dimension of b 3 times to turn b into a 100 x 200 x 3 array?
This is something akin to repmat in matlab. I basically want to triplicate the last dimension of b. I've tried np.tile(b, (1, 1, 3))
, but that yields the wrong dimensions.
Upvotes: 3
Views: 420
Reputation: 280291
a * b[..., np.newaxis]
Give b
another length-1 axis on the end, and broadcasting will handle this for you without needing to actually construct a tripled array.
Upvotes: 4