linkage — Construct a hierarchical agglomerative clustering tree.
linkage(X) computes pairwise distances between rows of X and returns an (m-1)-by-3 cluster tree. linkage(Y, method) accepts a row-vector condensed distance vector in the same ordering as pdist.
Syntax
Z = linkage(X)
Z = linkage(X, method)
Z = linkage(X, method, metric)
Z = linkage(X, method, metric, 'savememory', value)
Z = linkage(X, method, pdist_inputs)
Z = linkage(Y, method)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X_or_Y | NumericArray | Yes | — | Observation matrix X or condensed distance vector Y. |
options | Any | Variadic | — | Linkage method, distance metric, pdist metric parameters, and SaveMemory option. |
Returns
| Name | Type | Description |
|---|---|---|
Z | NumericArray | Hierarchical cluster tree with one merge per row. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:linkage:InvalidArgument | Inputs, linkage methods, distance metrics, or name-value options are malformed. | linkage: invalid argument |
RunMat:linkage:Internal | RunMat cannot allocate or construct the linkage output. | linkage: internal error |
How linkage works
- Rows of
Xare observations and columns are variables.Xmust contain at least two observations; ann-by-1 column vector is treated asnone-dimensional observations. - A row-vector input is interpreted as a condensed distance vector whose length is
n*(n-1)/2. - Distances must be finite and nonnegative.
- The default linkage method is
"single". - Supported methods are
"single","complete","average","weighted","centroid","median", and"ward", including common MATLAB aliases such as"nearest","farthest","upgma", and"wpgma". - Condensed distance vectors used with
"centroid","median", or"ward"must pass a Euclidean-distance validity check. linkage(X, method, metric, ...)uses the same named numeric distance metrics and metric parameters aspdist.linkage(X, method, {metric, parameter})accepts a cell array ofpdistinputs."SaveMemory","off"is accepted for source compatibility."SaveMemory","on"is accepted for observation-matrix input with centroid, median, or ward linkage and Euclidean distance; RunMat uses the same bounded in-memory implementation for both values.- The current exact agglomerative implementation is bounded to 700 observations to avoid unbounded CPU and memory use.
- The first two columns of
Zcontain one-based cluster identifiers. Original observations are numbered1:m; newly formed clusters are numberedm+1,m+2, and so on. The third column contains the linkage distance for each merge.
Examples
Cluster observation rows
X = [0 0; 3 4; 4 0; 0 2];
Z = linkage(X, "single")Expected output:
Z is a 3-by-3 hierarchy. The first merge joins observations 1 and 4 at distance 2.Cluster a condensed distance vector
Y = [1 4 6 5 7 2];
Z = linkage(Y, "complete")Expected output:
Z describes complete-linkage merges for four observations.Use a pdist metric
Z = linkage(X, "average", "cityblock")Expected output:
Pairwise cityblock distances are used before average-linkage clustering.Using linkage with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how linkage changes the result.
Run a small linkage example, explain the result, then change one input and compare the output.
FAQ
Does linkage draw a dendrogram?⌄
No. linkage returns the cluster tree matrix. Plotting functions such as dendrogram are separate compatibility surfaces.
Are custom distance functions supported?⌄
No. RunMat currently supports the standard named numeric distance metrics shared with pdist.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how linkage is executed, line by line, in Rust.
- View the source for linkage in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.