How to make elemental multiplication of a matrix of scalars with a matrix of vectors?

How can I convert the following numpy A array of form

[[1,2]
 [3,4]]

in type B

[[[1,1,1],[2,2,2]]
 [[3,3,3],[4,4,4]]]

what can i do element multiplication with C

[[[ 5, 6, 7],[ 8, 9,10]]
 [[11,12,13],[13,15,16]]]

?

The original problem is to multiply a scalar by a vector, for example. 4 * [13,15,16]. But instead of a scalar, I have a matrix of scalars (A), and instead of a vector, I have a matrix of vectors (C). If there is a way without real replication of values ​​like in B, I would prefer that (the obvious for-loop will be too slow, I think).

+4
source share
1 answer

You already mentioned the answer in the comments:

A[:,:,None] * C

, numpy None newaxis. :

newaxis . - newaxis .

, :

A.reshape(len(A), -1, 1)*C

, numpy array s, , , - .

+3

All Articles