User defined distance function

3 ビュー (過去 30 日間)
MByk
MByk 2017 年 7 月 2 日
コメント済み: MByk 2017 年 7 月 2 日
I'am trying to calculate Canberra distance (formula of the Canberra distance sum(a-b/|a|+|b|)) by defining a custom distance function but it is not working correctly. Would you help correcting my function? Thank you.
r = [0 3 4 5;7 6 3 -1;-1 1 -1 1;2 3 4 5];
dist = squareform(pdist(r,@f))
function dC = f(a,b)
[m,~]=size(b);
for i=1:m
dC = sum(abs(a - b(i,:))./(abs(a) + abs(b(i,:))));
end
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 2 日
編集済み: Andrei Bobrov 2017 年 7 月 2 日
r = [0 3 4 5;7 6 3 -1;-1 1 -1 1;2 3 4 5];
for MATLAB >= R2016b
dist = squareform(pdist(r,@(a,b)sum(abs(a - b)./(abs(a) + abs(b)),2)))
for MATLAB <= R2016a
f = @(a,b)sum(abs(bsxfun(@minus,a,b))./bsxfun(@plus,abs(a),abs(b)),2);
dist1 = squareform(pdist(r,f))
for your code
dist = squareform(pdist(r,@f))
function dC = f2(a,b)
[m,~] = size(b);
dC = zeros(m,1);
for ii=1:m
dC(ii) = sum(abs(a - b(ii,:))./(abs(a) + abs(b(ii,:))));
end
end
  1 件のコメント
MByk
MByk 2017 年 7 月 2 日
Thank you very much, it is working now. Much appreciated.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by