How to Find the Medoid of a Set in MATLAB

后端 未结 1 1303
既然无缘
既然无缘 2021-01-27 04:50

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

1条回答
  •  遥遥无期
    2021-01-27 05:17

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

    0 讨论(0)
提交回复
热议问题