Matlab to Python ( dot product and dot divide equivalents )

26 次查看(过去 30 天)
I am converting my matlab funtion to python. I want to rewrite this simple functions in python
function [ H ] = update_H( X , W , H )
H = H.*((W'*X)./secu_plus(W'*W*H,eps));
end
function [ W ] = update_W( X , W , H )
W = W.*((X*H')./secu_plus(W*(H*H'),eps));
end
Note: secu_plus is another function so ignore.
As you may see there are 2 kinds of multiplication * and .*, Also I have ./
so what are the equivalent forms in python [(.* ) (./ ) and (*) ]

回答(2 个)

M
M 2018-11-14

Fernando Feijoo
Fernando Feijoo 2020-2-24
I think that it's not possible a simple translation to python of the .* because the tratment of the operation is different in matlab and in Python. I hope this code in Python helps you
f=np.array([2,4])
t=np.array([1,2,3,4,5])
def funMatLabMultip(f,t):
"""create an n x m array from 2 vectors of size n and m.
Resulting rows are the multiplication of each element of the first vector for all the elements of the second vector
f=np.array([2,4])
t=np.array([1,2,3,4,5])
[[ 2 4 6 8 10]
[ 4 8 12 16 20]]
"""
if t.size==t.shape[0]:
k=f[0]*t
for i in f[1:]:
j=i*t
k=np.vstack((k,j))
else:
raise Exception('arrays should 1D arrays')
return k
k=funMatLabMultip(f,t)
print(k)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by