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