How to Find the Medoid of a Set in MATLAB

余生长醉 提交于 2019-12-02 09:56:00

问题


I am trying to calculate the medoid in matlab. However, i don't know how to do this. My dataset consists of multiple points of three-dimensional data (so a cloud of points in a system with three axes). The medoid is the point "whose average dissimilarity to all the other objects in the cluster is minimal" (wikipedia).

Does anyone know how to calculate the medoid in matlab?


Btw.: as far as i know the k-medoid algorithm cannot be used to calculate the medoid (efficiently), which is why i am looking for another way.


回答1:


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).



来源:https://stackoverflow.com/questions/9394744/how-to-find-the-medoid-of-a-set-in-matlab

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