File Exchange

image thumbnail

Efficient K-Means Clustering using JIT

version 1.0 (2.02 KB) by

A simple but fast tool for K-means clustering



View License

This is a tool for K-means clustering. After trying several different ways to program, I got the conclusion that using simple loops to perform distance calculation and comparison is most efficient and accurate because of the JIT acceleration in MATLAB.

The code is very simple and well documented, hence is suitable for beginners to learn k-means clustering algorithm.

Numerical comparisons show that this tool could be several times faster than kmeans in Statistics Toolbox.

Comments and Ratings (13)

provoid code

Junqi WANG

Nikolay S.

Nikolay S. (view profile)


leila (view profile)

Does the code support 3d data?



Maxime (view profile)

Although not a perfect way to solve the above-mentioned issue, adding the following two lines after the update of the centroids solved the problem in my case:

idnan = find(isnan(c(:,1)));
c(idnan,:) = X(randi(n,length(idnan),1),:);


Maxime (view profile)

Pretty fast indeed!

However, the number of cluster is sometimes not respected. The algorithm yields a lower number of clusters, replacing additional centroid by NaN. This can be inconvenient.

Tim Benham

The function fails to terminate on some inputs. For example see


Nandha (view profile)

The code is very nice and well documented. In some cases, however, the clusters are not properly identified if no initial centroid vectors are provided. This could be improved by automatically trying a small number of different random initial guesses and chosing the configuration which yields the smallest sum of distance between points and centroids.

V. Poor

Mo Chen

Mo Chen (view profile)

nicola rebagliati

this stuff works and examples/comparisons are given


update description

MATLAB Release
MATLAB 7.5 (R2007b)

Inspired: Patch color selector

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today