フィルターのクリア

convert code from a python function to matlab

1 回表示 (過去 30 日間)
Dany
Dany 2021 年 5 月 18 日
回答済み: Dany 2021 年 5 月 18 日
How to convert the following python code to matlab code?
below I send the code.
Thank you so much
# lambda L;
def J(X):
L=0.5
Xorigin=np.zeros([2,1])
JA=np.mean(np.sum(np.power(X-np.tile(Xorigin,(1,P)),2),axis=0))
JB=0
for i in range(0,P):
for j in range(i+1,P):
JB+=1/np.sum(np.power(X[:,i]-X[:,j],2))
JB=JB/(P*(P-1)/2)
return JA+L*JB
Function:

採用された回答

Walter Roberson
Walter Roberson 2021 年 5 月 18 日
X = randi([-9 9], 10, 2)
X = 10×2
7 -9 -7 -7 6 9 -5 6 -8 -6 -5 8 2 2 2 3 7 -6 6 -5
disp(J(X))
76.4701
function output = J(X)
L = 0.5;
S = size(X,1);
JA = sum(X.^2,2);
JB = sum(triu(squareform(1./pdist(X, 'squaredeuclidean'))),2);
output = sum(JA + JB)./S;
end
The equation you posted does not include a division by P*(P-1)/2 (for undefined variable P at that.)
The equation you posted is ambiguous about what the upper limit is for j. As s is not defined, it is possible that the size of X is greater than s, so the sum in the second part of the equation might include a potentially large number of terms.
The posted equation has j>=i but when j = i then is 0 and the term would be 1/0 which is infinity. That is not a useful equation, so we must suppose that j>i must be true. If s is the number of rows in X then for the last row, i = s, j>i would be empty, which leads to the question of whether adding emptiness should be treated the same as adding 0.

その他の回答 (1 件)

Dany
Dany 2021 年 5 月 18 日
Walter Roberson, you're right. I forgot to correct that observation. it is really (j > i).
Thank you so much..

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by