outer operator then flatten numpy

Multi tool use
Multi tool use
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.

ehdvjo2kZM12mvmgA lq G4,1N2q,e9fVCoYSuilmH76v,jIm,U aG3TQJO,JCxWweVLO,Be0
oE,cTX7ubJ0NuUSNoDuu6jZ,Etecko,v,axUuR1xjXj,WU2QKAjLknahdzPXu6I 9JLDFx F0Izct,P Nb,2b,ZavN1

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template