outer operator then flatten numpy

The name of the picture


outer operator then flatten numpy



Given two numpy arrays with dimensions (a,b) and (a,c) I would like to perform an outer operation (eg. add) flattened down to an array of shape (a,b*c).


a


b


a


c


a


b*c



I can do this using reshape:


reshape


# Input arrays:
array1 = np.arange(6).reshape(2,3)
array2 = np.arange(8).reshape(2,4)

output = (array1[:,None,:] + array2[...,None]).reshape(2,-1)



While this works, I was wondering if a solution not involving explicit reshaping exists? That is, do I have to do the intermediate step as an (a,b,c)-shaped array?


a


b


c





Reshaping is virtually free. That can't be the bottleneck.
– Divakar
4 hours ago





@Divakar True, this isn't a computational bottleneck. It just seems like something which could be solved in a nicer way
– M.T
3 hours ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size