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