I have the following numpy code. I have one array (a) with 3d points and another with weights (b). I need to multiply each row in a by each weight in b in the corresponding row. I am hoping to make this code more understandable and eliminate the loops.
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9, 10], [11, 12, 13, 14]])
c = np.zeros((2, 4, 3))
for i, row in enumerate(b):
for j, col in enumerate(row):
print('Mult:', a[i, :], '*', col)
c[i, j, :] = a[i, :] * col
print(c[0, :, :])
print(c[1, :, :])
Here's the output.
Mult: [1 2 3] * 7
Mult: [1 2 3] * 8
Mult: [1 2 3] * 9
Mult: [1 2 3] * 10
Mult: [4 5 6] * 11
Mult: [4 5 6] * 12
Mult: [4 5 6] * 13
Mult: [4 5 6] * 14
[[ 7. 14. 21.]
[ 8. 16. 24.]
[ 9. 18. 27.]
[10. 20. 30.]]
[[44. 55. 66.]
[48. 60. 72.]
[52. 65. 78.]
[56. 70. 84.]]