MATLAB Numpy element wise multiplication issue

Please see the MATLAB code and equivalent Numpy code below. Question: How can I get the D variable same in Numpy as MATLAB's?

MATLAB Code

A = [1 2 3; 4 5 6; 7 8 9]

C = [100 1; 10 0.1; 1, 0.01]

C = reshape(C, 1,3,2)

D = bsxfun(@times, A, C)

D(:,:,1) =

   100    20     3
   400    50     6
   700    80     9

D(:,:,2) =

1.0000    0.2000    0.0300
4.0000    0.5000    0.0600
7.0000    0.8000    0.0900

Numpy Code

A = np.array([[1,2,3],[4,5,6],[7,8,9]])

C = np.array([[[100, 1], [10, 0.1], [1, 0.01]]]) # C.shape is (1, 3, 2)

D = A * C.T

D

array([[[100.  , 200.  , 300.  ],
        [ 40.  ,  50.  ,  60.  ],
        [  7.  ,   8.  ,   9.  ]],

       [[  1.  ,   2.  ,   3.  ],
        [  0.4 ,   0.5 ,   0.6 ],
        [  0.07,   0.08,   0.09]]])


#python #numpy #matlab

2 Likes8.40 GEEK