[英]Is there an equivalent Matlab dot function in numpy?
Is there an equivalent Matlab dot
function in numpy? numpy中有等效的Matlab dot
函数吗?
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. Matlab中的dot
函数:对于多维数组A和B,点返回沿A和B的第一个非单维度的标量积。A和B的大小必须相同。
In numpy the following is similar but not equivalent: 在numpy中,以下内容相似但不等效:
dot (A.conj().T, B)
In MATLAB, dot(A,B)
of two matrices A
and B
of same size is simply: 在MATLAB中,具有相同大小的两个矩阵A
和B
的dot(A,B)
简单来说是:
sum(conj(A).*B)
Equivalent Python/Numpy: 等效的Python / Numpy:
np.sum(A.conj()*B, axis=0)
Matlab example1: Matlab example1:
A = [1,2,3;4,5,6] B = [7,8,9;10,11,12] dot(A,B)
Result: 47 71 99 结果:47 71 99
Matlab example2: Matlab example2:
sum(A.*B)
Result: 47 71 99 结果:47 71 99
Numpy version of Matlab example2: 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]]) 结果:矩阵([[47,71,99]])
Check these cheatsheets. 检查这些备忘单。
Numpy contains both an array class and a matrix class. Numpy同时包含数组类和矩阵类。 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. 数组类旨在成为用于多种数值计算的通用n维数组,而矩阵旨在专门帮助线性代数计算。 In practice there are only a handful of key differences between the two. 实际上,两者之间只有几个关键区别。
Operator
*
, dot(), and multiply(): 运算符*
,dot()和乘法():
For array,*
means element-wise multiplication, and the dot() function is used for matrix multiplication. 对于数组,*
表示逐元素乘法,而dot()函数用于矩阵乘法。
For matrix,*
means matrix multiplication, and the multiply() function is used for element-wise multiplication. 对于矩阵,*
表示矩阵相乘,而multiple()函数用于按元素相乘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.