Categories &

Functions List

Function Reference: kmedoids

statistics: idx = kmedoids (X, k)
statistics: [idx, C] = kmedoids (X, k)
statistics: [idx, C, sumd] = kmedoids (X, k)
statistics: [idx, C, sumd, D] = kmedoids (X, k)
statistics: [idx, C, sumd, D, midx] = kmedoids (X, k)
statistics: [idx, C, sumd, D, midx, info] = kmedoids (X, k)
statistics: […] = kmedoids (…, name, value)

Partition observations into k clusters using the k-medoids algorithm.

idx = kmedoids (X, k) partitions the N×P numeric matrix X into k clusters, each represented by one of the observations (its medoid), and returns the N×1 vector idx of cluster indices. Rows of X correspond to observations and columns correspond to features or variables. Unlike kmeans, whose centroids are the mean of each cluster, a medoid is an actual data point, which makes k-medoids more robust to outliers and applicable to any distance metric.

[idx, C, sumd, D, midx, info] = kmedoids (…) returns additional results:

Ca k×P matrix with the coordinates of the k medoids, one per row (C = X(midx,:)).
sumda k×1 vector with the within-cluster sum of the distances from each point to its cluster medoid, measured with the selected metric.
Dan N×k matrix with the distance from every observation to every medoid.
midxa k×1 vector with the row indices into X of the k medoids.
infoa scalar structure with the fields 'algorithm', 'start', 'distance', 'iterations', and 'bestReplicate' describing the chosen run.

Source Code: kmedoids

Additional parameters can be specified by Name-Value pair arguments.

NameValue
'Distance'the distance metric, one of 'sqeuclidean' (default), 'euclidean', 'seuclidean', 'cityblock', 'minkowski', 'chebychev', 'cosine', 'correlation', 'hamming', 'jaccard', 'spearman', 'mahalanobis', or a custom distance function handle accepted by pdist2.
'Algorithm'the optimization algorithm, either 'pam' (default) for Partitioning Around Medoids, which searches over all medoid/non-medoid swaps, or 'small' for the faster Voronoi iteration that reassigns points and re-selects each cluster medoid until convergence.
'Start'the method used to choose the initial medoids: 'plus' (default, k-means++), 'sample' (a random subset of the observations), 'cluster' (a preliminary pass on a subsample), or a k×P numeric matrix of starting medoid locations, each snapped to the nearest observation. A k×P×R array supplies a separate start for each of R replicates.
'Replicates'a positive integer number of times to repeat the clustering, each with a new set of initial medoids; the solution with the lowest total sum of distances is returned. The default is 1, or the size of the third dimension of a numeric 'Start'.
'Options'a structure, as created by statset, whose 'MaxIter' field caps the number of iterations (default 100).

Source Code: kmedoids

See also: kmeans, linkage, pdist2, dbscan

Source Code: kmedoids

Cluster three noisy blobs and mark the medoids.

 X = [randn(20,2)*0.4 + 3; randn(20,2)*0.4; randn(20,2)*0.4 + [3 -3]];
 [idx, C] = kmedoids (X, 3);
 gscatter (X(:,1), X(:,2), idx);
 hold on;
 plot (C(:,1), C(:,2), "kp", "MarkerSize", 14, "MarkerFaceColor", "y");
 hold off;
 title ("kmedoids: three clusters with their medoids");
plotted figure