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

pdist2 — Compute pairwise distances between two sets of observations.

pdist2(X,Y) returns a size(X,1) by size(Y,1) matrix of distances between rows of X and rows of Y.

Syntax

D = pdist2(X, Y)
D = pdist2(X, Y, Distance, DistParameter, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
XNumericArrayYes—Observation matrix with observations in rows.
YNumericArrayYes—Second observation matrix with observations in rows.
optionsAnyVariadic—Distance metric, metric parameter, or Smallest/Largest selection options.

Returns

NameTypeDescription
DNumericArrayPairwise distances.
DNumericArraySelected pairwise distances.
INumericArrayOne-based row indices from X for selected distances.

Returned values from pdist2 depend on how many outputs the caller requests.

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 pdist2 works

  • X and Y must be real numeric vectors or 2-D matrices with the same number of columns.
  • The default metric is Euclidean distance.
  • Supported metrics match pdist: "euclidean", "squaredeuclidean", "cityblock", "chebychev", "minkowski", "seuclidean", "mahalanobis", "cosine", "correlation", "hamming", "jaccard", and "spearman".
  • pdist2(X,Y,"minkowski",P) uses positive finite exponent P.
  • pdist2(X,Y,"seuclidean",S) accepts a standard-deviation vector and defaults to scale estimated from X. pdist2(X,Y,"mahalanobis",C) accepts a symmetric positive definite covariance matrix and defaults to covariance estimated from X.
  • "Smallest",K and "Largest",K return the K smallest or largest distances per row of Y; when K exceeds the number of rows in X, all X rows are returned. [D,I] = pdist2(...) also returns one-based row indices from X for the selected distances.
  • Typed-integer observation data, distance parameters, and CacheSize values are checked RunMat floating-boundary extensions. Typed Smallest and Largest counts are separate structural extensions decoded directly from native storage.

Examples

Compute distances between two sets

X = [0 0; 2 0];
Y = [1 0; 3 0];
D = pdist2(X,Y)

Expected output:

D is a 2-by-2 distance matrix.

Use squared Euclidean distance

D = pdist2(X,Y,"squaredeuclidean")

Expected output:

D contains squared distances.

Keep nearest distances per query row

[D,I] = pdist2(X,Y,"euclidean","Smallest",1)

Expected output:

D is a 1-by-size(Y,1) matrix of nearest distances; I contains row indices from X.

Using pdist2 with coding agents

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

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

FAQ

Does Smallest return indices?⌄

Yes. Request [D,I] to receive one-based row indices from X, matching MATLAB's nearest-distance output form.

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 · pdist · 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 pdist2 is executed, line by line, in Rust.

  • View the source for pdist2 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 pdist2 works
  • Examples
  • Compute distances between two sets
  • Use squared Euclidean distance
  • Keep nearest distances per query row
  • Using pdist2 with coding agents
  • FAQ
  • Related Stats functions
  • Ml
  • Summary
  • Random
  • Hist
  • Options
  • Open-source implementation
  • About RunMat