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 the lower-left distance triangle: (2,1), (3,1), ..., (n,1), (3,2), ....
Syntax
D = pdist(X)
D = pdist(X, Distance, DistParameter)Inputs
X- Type
NumericArray- Required
- Yes
- Description
- Observation matrix with observations in rows.
options- Type
Any- Required
- Variadic
- Description
- Distance metric, optional metric parameter, or CacheSize.
Returns
D- Type
NumericArray- Description
- Pairwise distances in double precision.
Errors
RunMat:distance:InvalidArgument- When
- Distance inputs, dimensions, metrics, or controls are malformed.
- Message
distance helper: invalid argument
RunMat:distance:Internal- When
- RunMat cannot allocate or construct a distance output.
- Message
distance helper: internal error
Observations and metrics
X must be a real numeric vector or two-dimensional matrix. Rows are observations and columns are variables. The default metric is Euclidean distance.
Supported named metrics are euclidean, squaredeuclidean, cityblock, chebychev, minkowski, seuclidean, mahalanobis, cosine, correlation, hamming, jaccard, and spearman. Custom distance functions are not supported.
pdist(X,"minkowski",P) uses a positive finite exponent. pdist(X,"seuclidean",S) uses a positive finite standard-deviation vector; without S, RunMat estimates column variances from X, omitting NaNs from scale estimation. pdist(X,"mahalanobis",C) accepts a covariance matrix; without C, RunMat computes and inverts covariance from X.
Classes and execution
Results are double row vectors. Typed-integer observation data, distance parameters, and CacheSize are independently gated RunMat extensions. They must be exactly representable at the binary64 distance boundary. CacheSize is validated for compatibility but does not change the present CPU algorithm.
Resident inputs are gathered through their owning provider and pairwise distances run on CPU. This is a materializing fusion boundary, not a GPU distance kernel.
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
X = [0 0; 3 4; 4 0; 0 2]; D = pdist(X,"cityblock")Expected output:
D contains Manhattan distances between row pairs.Use Minkowski distance
X = [0 0; 3 4; 4 0]; 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?⌄
Condensed form avoids duplicating a symmetric matrix. Use squareform to expand it.
Are custom distance functions supported?⌄
No. RunMat currently supports the named numeric distance metrics listed above.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · nlparci · optimizableVariable · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
anova1 · binocdf · binscatter · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · ksdensity · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pca · pdf · prctile · qqplot · quantile · refline · rmse · scatterhist · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · gpurng · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.