I have a numpy array A, which has shape (10,).
I also have, as of this moment, a numpy array B with shape (10,3,5). I want to do a multiplication between these two to get C such that C[0,:,:]=A[0]*B[0,:,:], C[1]=A[1]*B[1,:,:], etc.
I do not want to work this out with loops, one reason being the aesthetics of the thing, the other being that this code needs to be very generic. I want the user to be able to input pretty much any B of any shape as long as the leading dimension is 10. For instance, I want the user to be able to also put in a B of shape (10,4).
So: How can I implement this multiplication using numpy? Thanks.
ADDENDUM: Have been asked for example. Will go smaller. Let's say A is the numpy array [1,2,3] and B is the numpy array [[1,2],[4,5],[7,8]]. I want the multiplication of the two to result in [[1,2],[8,10],[21,24]]. ...
>>> a
array([1, 2, 3])
>>> b
array([[1, 2],
[4, 5],
[7, 8]])
>>> #result
>>> c
array([[ 1, 2],
[ 8, 10],
[21, 24]])
>>>
Bis(3,5,10), theA*Bworks.numpyautomatically adds dimensions at the start as needed (MATLAB adds them at the end).(A*B.T).Twould match up the length 10s for multiplication and would be a general solution, but I think theeinsumapproach as suggested by DSM is arguably nicer.