In MATLAB, I do a calculation using
repmat(A-B,100,1).*rand(100,length(B))
Here, A
and B
are 1*19-sized matrices.
To execute the same code in Python, I am using the following code-
np.matmul(np.matlib.repmat(A - B, 100, 1), np.matlib.rand(100, len(B)))
Upon running the code, I get the following error-
ValueError: matmul:
Input operand 1 has a mismatch in its core dimension 0,
with gufunc signature (n?,k),(k,m?)->(n?,m?)
(size 100 is different from 19)
What should I do?
CodePudding user response:
MATLAB's .*
is a broadcasting operator. It scalar-extends the *
operator to apply to matrices of matching size pointwise. This is not matrix multiplication, which is a different operation available on matrices that requires the middle two dimensions to match.
The equivalent of .*
in Python, assuming the left-hand and right-hand side are numpy
arrays, is simply *
.
np.matlib.repmat(A - B, 100, 1) * np.matlib.rand(100, len(B))
If the left-hand and right-hand side are not numpy
arrays (for instance, if they're ordinary Python lists), then you can convert them by calling numpy.array
on them beforehand