Home > Mobile >  multiplication of uneven python arrays
multiplication of uneven python arrays

Time:09-21

Im attempting to multiply arrays, however in this case they are uneven, and I need to retain the 7 and the 10 in the output.

import numpy as np

m = np.array([[1,2],[3,4]])
c = np.array([[5,6,7],[8,9,10]])
m * c
#Expected output

[5,12,7],
[24,36,10],

I think I need to reshape the arrays? Any assistance would be appreciated. Cheers

CodePudding user response:

Make a copy and multiply on the slice:

>>> res = c.copy()
>>> res[:, :2] *= m
>>> res
array([[ 5, 12,  7],
       [24, 36, 10]])

CodePudding user response:

'*' is used for something else, you can use dot or matmul for matrix multiplication which yields as follows

import numpy as np

m = np.array([[1, 2], [3, 4]])
c = np.array([[5, 6, 7], [8, 9, 10]])
print(np.dot(m, c))
print(" ")
print(np.matmul(m, c))
[[21 24 27]
 [47 54 61]]

[[21 24 27]
 [47 54 61]]

#EDIT 1
I read the question wrong and assumed it was matrix multiplication but its array multiplication

  • Related