pdist2
Pairwise distances between two sets of observations
Syntax
D = pdist2(X, Y) D = pdist2(X, Y, metric) D = pdist2(X, Y, metric, param)
Arguments
- X
n x p real matrix. Each row stores one observation with p features.
- Y
m x p real matrix. Must have the same number of columns as
X.- metric
optional string selecting the distance metric. If omitted, the Euclidean distance is used.
- param
optional real input used only by
"seuclidean","mahalanobis"and"minkowski"(see Description).- D
n x m real matrix in which
D(i,j)stores the distance betweenX(i,:)andY(j,:).
Description
pdist2 computes the distances between every row of X and every row of Y with the selected metric,
returning the n-by-m distance matrix. It is typically used when distances between two sets of points are required
(for example, classification, nearest-neighbour search, kernels or clustering initialisation).
The following metric names are supported (aliases in parentheses):
"euclidean"("euclid","eu","e"): Euclidean (L2) distance."squaredeuclidean"("sqeuclidean","sqe","sqeuclid"): squared L2 distance."seuclidean"("se","s"): standardized Euclidean distance."mahalanobis"("mahal","mah"): Mahalanobis distance."cityblock"("city","city block","cblock","cb","c"): city-block / Manhattan distance."minkowski"("mi","m"): Minkowski distance of orderparam."chebychev"("chebyshev","cheby","cheb","ch"): Chebyshev (L∞) distance."cosine"("cos"): cosine distance (1 minus cosine similarity)."correlation"("co"): correlation distance (1 minus sample correlation)."hamming"("hamm","ha","h"): fraction of coordinates with different values."jaccard"("jacc","ja","j"): Jaccard distance designed for binary-like data."canberra": Canberra distance (weighted absolute differences)."braycurtis": Bray-Curtis dissimilarity.
When metric is "seuclidean", param must be a 1 x p row vector providing the scale (for example, the
standard deviation of each column). If it is omitted, stdev(X, "r") is computed automatically and zero entries are replaced by 1.
For "mahalanobis", param must be a p x p covariance matrix. When omitted, cov(X) is computed and inverted.
For "minkowski", param must be a positive scalar that gives the order of the norm (defaults to 2).
Providing param for any other metric has no effect (a warning is displayed).
Examples
Euclidean distances between two point clouds
X = [0 0; 1 0]; Y = [0 1; 1 1; 2 2]; D = pdist2(X, Y) // [1.000000 1.414214 2.828427; 1.414214 1.000000 2.236068]
Standardized Euclidean distance with custom scales
X = [1 2; 3 4]; Y = [2 0]; scale = [0.5 2]; D = pdist2(X, Y, "seuclidean", scale) // [2.2360680; 2.8284271]
See also
History
| バージョン | 記述 |
| 2026.1.0 | Function added. |
| Report an issue | ||
| << pdist | Statistics | polyfit >> |