0
matrixADimensions = matrixA.shape # returns [901,1249,1]
matrixBDimensions = matrixB.shape # returns [901,1249]

I am trying to get the element-wise multiplication of matrixA and matrixB but I am getting the error ValueError: operands could not be broadcast together with shapes (901,1249,1) (901,1249).

I believe it has something to do with the dimensions of both matrices since they are not the same. Actually, technically they are the same since [901,1249,1] is the same thing as [901,1249] but Python does not seem to know this.

How can I multiply matrixA with matrixB?

user1251007
  • 15,891
  • 14
  • 50
  • 76
Senyokbalgul
  • 1,058
  • 4
  • 13
  • 37

1 Answers1

1

You can use numpy.squeeze to remove single-dimensional entries from the shape of your array. So in your case, you would do:

import numpy as np

np.squeeze(matrixA) * matrixB

This has the advantage of not needing to know the position of your single-dimensional entry in your array shape (unlike taking an indexing approach such as matrixA[:,:,0]).

Andrew Guy
  • 9,310
  • 3
  • 28
  • 40