0

I have a (n,1) np-array, for example array([1 2 3]) that I would like to multiply element wise with a np-matrix (n,m), for example array([[1 1 1], [2 2 2], [3 3 3]]) so that I will get:

array([[1 1 1], [4 4 4], [9 9 9]])

How can I do that?

I have tried with np.multiply and np.dot.

Emelie
  • 31
  • 4
  • 1
    Duplicate of https://stackoverflow.com/questions/18522216/multiplying-across-in-a-numpy-array – Chgad Oct 11 '19 at 12:58

1 Answers1

1

Reshape your vector so that it contains 3 rows instead of 3 columns:

v = np.array([1, 2, 3])
m = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])

u = v.reshape(*v.shape, 1)
u * m  # results in [[1, 1, 1], [4, 4, 4], [9, 9, 9]]
jfaccioni
  • 7,099
  • 1
  • 9
  • 25