# Matlab to Python ( dot product and dot divide equivalents )

185 views (last 30 days)
fadams18 on 13 Nov 2018
Answered: Fernando Feijoo on 24 Feb 2020
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 (*) ]

M on 14 Nov 2018

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:
k=f*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)
{"ft_lang_label":"__label__en","ft_lang_prob":0.73876226,"math_prob":0.965025,"size":1469,"snap":"2022-40-2023-06","text_gpt3_token_len":452,"char_repetition_ratio":0.09829351,"word_repetition_ratio":0.03984064,"special_character_ratio":0.33492172,"punctuation_ratio":0.1522388,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9984342,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-07T13:37:43Z\",\"WARC-Record-ID\":\"<urn:uuid:51beaaf7-4b36-4e1e-8030-e3a713dae094>\",\"Content-Length\":\"131127\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f4b88490-f7fb-4cb0-b2ca-8c77a895cfb6>\",\"WARC-Concurrent-To\":\"<urn:uuid:912cea74-a284-4757-913a-55f48caf59ee>\",\"WARC-IP-Address\":\"23.1.9.244\",\"WARC-Target-URI\":\"https://fr.mathworks.com/matlabcentral/answers/429657-matlab-to-python-dot-product-and-dot-divide-equivalents\",\"WARC-Payload-Digest\":\"sha1:RZR4IUMXJCPAEDHBSZ4S2MEXRPNXH5I3\",\"WARC-Block-Digest\":\"sha1:WSNYMS4MOBYNLGGV6JMQDWWD6V4KRZT6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030338073.68_warc_CC-MAIN-20221007112411-20221007142411-00149.warc.gz\"}"}