Reputation: 3761
Is there an equivalent Matlab dot
function in numpy?
The dot
function in Matlab:
For multidimensional arrays A and B, dot returns the scalar product along the first non-singleton dimension of A and B. A and B must have the same size.
In numpy the following is similar but not equivalent:
dot (A.conj().T, B)
Upvotes: 5
Views: 6422
Reputation: 137594
Check these cheatsheets.
Numpy contains both an array class and a matrix class. The array class is intended to be a general-purpose n-dimensional array for many kinds of numerical computing, while matrix is intended to facilitate linear algebra computations specifically. In practice there are only a handful of key differences between the two.
Operator
*
, dot(), and multiply():
For array,*
means element-wise multiplication, and the dot() function is used for matrix multiplication.
For matrix,*
means matrix multiplication, and the multiply() function is used for element-wise multiplication.
Upvotes: -1
Reputation: 1437
Matlab example1:
A = [1,2,3;4,5,6]
B = [7,8,9;10,11,12]
dot(A,B)
Result: 47 71 99
Matlab example2:
sum(A.*B)
Result: 47 71 99
Numpy version of Matlab example2:
A = np.matrix([[1,2,3],[4,5,6]])
B = np.matrix([[7,8,9],[10,11,12]])
np.multiply(A,B).sum(axis=0)
Result: matrix([[47, 71, 99]])
Upvotes: 0
Reputation: 124563
In MATLAB, dot(A,B)
of two matrices A
and B
of same size is simply:
sum(conj(A).*B)
Equivalent Python/Numpy:
np.sum(A.conj()*B, axis=0)
Upvotes: 10