How to Find the Medoid of a Set in MATLAB

泄露秘密 提交于 2019-12-02 07:27:35

Should not be difficult to do that once you provide the metric. Here is an implementation for scalars:

     function m = medoid(set,metric)
          [X,Y] = meshgrid(set,set); %Create all possible pairs
          dist = metric(X,Y);  %Run metric

          %Each distance is calculated twice, that doesn't matter. 
          %Also addition of zeros doesn't matter because we are looking for minimum.
          totalDist = mean(dist,1); 

          [~,i] = min(totalDist);
          m = set(i);
     end

And the use case:

metric = @(x,y) ( abs(x-y));
m = medoid([1 2 3 3 3 3 3], metric)

You can extend it to vectors, I will leave it as exercise for the reader. (Or someone who wants to add an improved answer).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!