Scilab Website | Contribute with GitLab | Scilab Community | ATOMS toolboxes
Scilab Online Help
2026.1.0 - Português


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 between X(i,:) and Y(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 order param.

  • "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

  • pdist — Pairwise distances between observations
  • dbscan — Density-based clustering (DBSCAN)
  • kmeans — K-means clustering

History

VersãoDescrição
2026.1.0 Function added.
Report an issue
<< pdist Estatística polyfit >>

Copyright (c) 2022-2026 (Dassault Systèmes S.E.)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue May 19 14:03:50 CEST 2026