File Exchange

## Kmedoids

version 1.5 (2.78 KB) by Mo Chen

### Mo Chen (view profile)

Very fast matlab implementation of kmedoids clustering algorithm

Updated 23 Jan 2019

This is a fully vectorized version kmedoids clustering methods (http://en.wikipedia.org/wiki/K-medoids). It is usually more robust than kmeans algorithm. Please try following code for a demo:
close all; clear;
d = 2;
k = 3;
n = 500;
[X,label] = kmeansRnd(d,k,n);
y = kmedoids(X,k);
plotClass(X,label);
figure;
plotClass(X,y);
Input data are assumed COLUMN vectors!
You can only visualize 2d data!
This function is now a part of the PRML toolbox (http://www.mathworks.com/matlabcentral/fileexchange/55826-pattern-recognition-and-machine-learning-toolbox)

### Cite As

Mo Chen (2019). Kmedoids (https://www.mathworks.com/matlabcentral/fileexchange/28898-kmedoids), MATLAB Central File Exchange. Retrieved .

Mo Chen

### Mo Chen (view profile)

@Jakob Dohrmann you are right

Jakob Dohrmann

### Jakob Dohrmann (view profile)

I believe:
D(sub2ind([d,d],1:d,1:d)) = 0; % reduce chance of numerical problems
should be:
D(sub2ind([n,n],1:n,1:n)) = 0; % reduce chance of numerical problems
This sets the diagonal of the n x n (NOT d x d) matrix D to zero.

Also, for those running on ancient versions of matlab:
D = v+v'-2*(X'*X);
needs to be replaced with
D = -2*(X'*X); % Matlab <2016
D = bsxfun(@plus, D, v);
D = bsxfun(@plus, D, v');

Prasanta Saikia

### Prasanta Saikia (view profile)

It was giving me the following error:

Error using +
Matrix dimensions must agree.

Error in kmedoids (line 20)
D = v+v'-2*(X'*X); % Euclidean distance matrix

D = v+v'-2*(X'*X); % Euclidean distance matrix
it should be:
D = v'*v-2*(X'*X); % Euclidean distance matrix

And that fixed the issue for me.

Mo Chen

### Mo Chen (view profile)

@yu dengxiu
@saeed safarian
This function requires Matlat version >= R2016b. X = X-mean(X,2) is a new syntax of R2016b. You can change this to X=bsxfun(@minus, X, mean(X,2)); for older version of Matlab

yu dengxiu

### yu dengxiu (view profile)

thanks for this code, but it doesent work correctly.
X = X-mean(X,2) in kmedoids function has an error that Matrix dimensions must agree.
How to fix it?

saeed safarian

### saeed safarian (view profile)

thanks for this code, but it doesent work correctly.
X = X-mean(X,2) in kmedoids function has an error that Matrix dimensions must agree.

Mark Ebden

### Mark Ebden (view profile)

Thanks for this code, but for some datasets it's hypersensitive to rounding errors: occasionally the slightly nonzero entries along D's diagonal lead to results that are surprisingly far from correct (eg the chance that given medoids are chosen increases or decreases). I can email code to demonstrate if needed.

The problem is fixed by adding a for-loop after D is defined:

for i=1:n,
D(i,i) = 0;
end

To confirm that this yields 'correct' results, I perturbed one of the problematic datasets by a tiny amount and compared medoids before-and-after.

Hope that helps.

Eelke Spaak

### Eelke Spaak (view profile)

Even though the code is lightning fast, the solution is not the proper one, hence this code is useless. See http://i.imgur.com/VNY73l7.png for example output of k = 6 for a naive (and very slow) implementation of the algorithm, and this submission. Obviously the naive is correct, this submission is incorrect.

Nonetheless thanks for the effort! It would be great if you could produce correct code that is still as fast.

Yuanjun Xiong

kalarmago

kalarmago

### kalarmago (view profile)

I tested calling to kmedoids() function, it always returns
label = 2 1
energy = 0
index = 2 1
I believe it does not work. Thanks anyway.

SU Li

### SU Li (view profile)

Nice code. Very helpful. However, if the number of input data is large, then the D matrix is too large (n^2) which may make memory overflow.

Joao Henriques

### Joao Henriques (view profile)

Simple and elegant code, thanks!

Tong

### Tong (view profile)

If I want to use the cosine distance between two vectors, What shold I do?

MUSA

kannan

### kannan (view profile)

if i want to apply kmedoids agorithm for X data in function [label, energy, index] = kmedoids(X,k) 3599*11 size data then it is not working properly can anybody of you give idea for this?

Nejc Ilc

### Nejc Ilc (view profile)

Very compact and efficient coding. Nice job!
However, there is an error when k=1. I suggest to add the third parameter (dimension) to the calls of function min:
(Line 8): [~, label] = min(D(randsample(n,k),:),[],1);
(Line 11): [~, label] = [~, index] = min(D*sparse(1:n,label,1,n,k,n),[],1);

Tu

### Tu (view profile)

Not working for my data as well.
My data was of the dimension 17-by-71.
wanted to find 4 or 6 clusters.

swaheed.iu@gmail.com

Graeme

### Graeme (view profile)

Undefined function 'randsample' for input arguments of type 'double'.

Error in kmedoids (line 8)
[~, label] = min(D(randsample(n,k),:));

huang

Mo Chen

### Mo Chen (view profile)

Dinusha Rathnayaka

### Dinusha Rathnayaka (view profile)

Dear Sir Pls help!!! My Matlab version is Matlab7.6.0..Is this the reason?

Dinusha Rathnayaka

### Dinusha Rathnayaka (view profile)

SIMPLY NOT working.
label=kmedoids(x,3);