pdist — Compute pairwise distances between rows of an observation matrix.

pdist(X) returns a condensed row vector containing distances between every pair of rows in X, ordered like MATLAB's lower-left distance triangle: (2,1), (3,1), ..., (n,1), (3,2), ....

Syntax

D = pdist(X)
D = pdist(X, Distance, DistParameter)

Inputs

NameTypeRequiredDefaultDescription
XNumericArrayYesObservation matrix with observations in rows.
optionsAnyVariadicDistance metric and optional metric parameter.

Returns

NameTypeDescription
DNumericArrayPairwise distances.

Errors

IdentifierWhenMessage
RunMat:distance:InvalidArgumentInputs, dimensions, metrics, metric parameters, or selection options are malformed.distance helper: invalid argument
RunMat:distance:InternalRunMat cannot allocate or construct a distance output.distance helper: internal error

How pdist works

  • X must be a real numeric vector or 2-D matrix. Rows are observations and columns are variables.
  • The default metric is Euclidean distance.
  • Supported metrics are "euclidean", "squaredeuclidean", "cityblock", "chebychev", "minkowski", "seuclidean", "mahalanobis", "cosine", "correlation", "hamming", "jaccard", and "spearman".
  • pdist(X,"minkowski",P) uses positive finite exponent P.
  • pdist(X,"seuclidean",S) uses positive finite standard-deviation vector S; without S, RunMat computes column variances from X with NaNs omitted from scale estimation.
  • pdist(X,"mahalanobis",C) uses covariance matrix C; without C, RunMat computes the covariance from X and inverts it.

Examples

Compute Euclidean distances

X = [0 0; 3 4; 4 0; 0 2];
D = pdist(X)

Expected output:

D starts with [5 4 2], the distances from rows 2, 3, and 4 to row 1.

Use a cityblock metric

D = pdist(X, "cityblock")

Expected output:

D contains Manhattan distances between row pairs.

Use Minkowski distance

D = pdist(X, "minkowski", 3)

Expected output:

D contains pairwise L3 distances.

Using pdist with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how pdist changes the result.

Run a small pdist example, explain the result, then change one input and compare the output.

FAQ

Why is the output a row vector?

MATLAB stores pairwise distances in condensed form to avoid duplicating the symmetric matrix. Use squareform to expand the vector.

Are custom distance functions supported?

No. RunMat currently supports the standard named numeric distance metrics.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how pdist is executed, line by line, in Rust.

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.