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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | NumericArray | Yes | — | Observation matrix with observations in rows. |
options | Any | Variadic | — | Distance metric and optional metric parameter. |
Returns
| Name | Type | Description |
|---|---|---|
D | NumericArray | Pairwise distances. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:distance:InvalidArgument | Inputs, dimensions, metrics, metric parameters, or selection options are malformed. | distance helper: invalid argument |
RunMat:distance:Internal | RunMat cannot allocate or construct a distance output. | distance helper: internal error |
How pdist works
Xmust 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 exponentP.pdist(X,"seuclidean",S)uses positive finite standard-deviation vectorS; withoutS, RunMat computes column variances fromXwith NaNs omitted from scale estimation.pdist(X,"mahalanobis",C)uses covariance matrixC; withoutC, RunMat computes the covariance fromXand inverts it.- Typed-integer observation data, distance parameters, and
CacheSizevalues are independent RunMat extensions and must be exactly representable at the binary64 distance boundary.CacheSizeis validated for compatibility but does not change the current CPU algorithm.
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.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · 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 pdist is executed, line by line, in Rust.
- View the source for pdist 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.