Is there an equivalent Matlab dot function in numpy?

Question:

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)
Asked By: Kicsi Mano

||

Answers:

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)
Answered By: Amro

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.

Answered By: Colonel Panic

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]])

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.