RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • bayesopt
    • classify
    • confusionmat
    • crossvalind
    • cvpartition
    • fitclinear
    • fitctree
    • fitlm
    • kmeans
    • knnsearch
    • lasso
    • lassoglm
    • linkage
    • lscov
    • mnrfit
    • optimizableVariable
    • pdist
    • pdist2
    • perfcurve
    • predict
    • regress
    • ridge
    • squareform
    • test
    • training
    • tsne

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

NameTypeRequiredDefaultDescription
X_or_YNumericArrayYes—Observation matrix X or condensed distance vector Y.
optionsAnyVariadic—Linkage method, distance metric, pdist metric parameters, and SaveMemory option.

Returns

NameTypeDescription
ZNumericArrayHierarchical cluster tree with one merge per row.

Errors

IdentifierWhenMessage
RunMat:linkage:InvalidArgumentInputs, linkage methods, distance metrics, or name-value options are malformed.linkage: invalid argument
RunMat:linkage:InternalRunMat cannot allocate or construct the linkage output.linkage: internal error

How linkage works

  • Rows of X are observations and columns are variables. X must contain at least two observations; an n-by-1 column vector is treated as n one-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 as pdist.
  • linkage(X, method, {metric, parameter}) accepts a cell array of pdist inputs.
  • "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 Z contain one-based cluster identifiers. Original observations are numbered 1:m; newly formed clusters are numbered m+1, m+2, and so on. The third column contains the linkage distance for each merge.
  • Typed-integer observations, condensed distances, and numeric distance parameters are separately gated RunMat extensions and must be exactly representable at the binary64 clustering boundary.

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

histc · histcounts · histcounts2

Options

statget · statset

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How linkage works
  • Examples
  • Cluster observation rows
  • Cluster a condensed distance vector
  • Use a pdist metric
  • Using linkage with coding agents
  • FAQ
  • Related Stats functions
  • Ml
  • Summary
  • Random
  • Hist
  • Options
  • Open-source implementation
  • About RunMat