Matlab to Python ( dot product and dot divide equivalents )

57 views (last 30 days)
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 (*) ]

Answers (2)

M
M on 14 Nov 2018

Fernando Feijoo
Fernando Feijoo on 24 Feb 2020
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)

Tags

Products

Community Treasure Hunt

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

Start Hunting!