Matlab to Python ( dot product and dot divide equivalents )

조회 수: 57 (최근 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 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