简体   繁体   中英

Is there an equivalent Matlab dot function in numpy?

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)

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM