Matlab to Python ( dot product and dot divide equivalents )

18 ビュー (過去 30 日間)
fadams18
fadams18 2018 年 11 月 13 日
回答済み: Fernando Feijoo 2020 年 2 月 24 日
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 Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by