DistanceMatrix

DistanceMatrix[{u1,u2,}]

gives the matrix of distances between each pair of elements ui, uj.

DistanceMatrix[{u1,u2,},{v1,v2,}]

gives the matrix of distances between each pair of elements ui, vj.

Details and Options

Examples

open allclose all

Basic Examples  (3)

Compute a distance matrix from a list of integers:

Compute a distance matrix from two lists of integers:

Compute a distance matrix from real-valued numerical vectors:

Scope  (10)

Compute a distance matrix from images:

Compute a distance matrix from strings:

Compute a distance matrix from Boolean vectors:

Compute a distance matrix from a list of date objects:

Compute a distance matrix from geodetic positions:

Compute a distance matrix from nominal sequences:

Compute a distance matrix from numerical sequences:

Compute a distance matrix on nominal vectors:

Compute a distance matrix from mixed-type vectors:

Compute a distance matrix from a dataset formatted as a list of associations:

Compute the same distance matrix with a column-oriented dataset:

Data can also be given in a Dataset object:

Options  (9)

DistanceFunction  (3)

Compute a distance matrix from integer vectors using SquaredEuclideanDistance as a distance function:

Compute a distance matrix with the ManhattanDistance:

Use a color distance different from the default distance:

FeatureExtractor  (1)

Compute a distance matrix from images preprocessed by the feature extractor method "NumericVector":

FeatureNames  (1)

Use FeatureNames to name features, and refer to their names in further specifications:

FeatureTypes  (1)

Use FeatureTypes to enforce the interpretation of the first feature as nominal:

PerformanceGoal  (1)

Generate 2000 random numerical vectors of length 1000:

Compute their distance matrix and benchmark the operation:

Perform the same operation with PerformanceGoal set to "Speed":

Compare timing and accuracies of the previous results with a reference:

When PerformanceGoal"Speed", centering the data can increase the precision:

RandomSeeding  (1)

DistanceMatrix gives the same result when evaluated multiple times, even when randomness is involved.

Generate a pair of 20-dimensional vectors:

Compute its distance matrix several times using a feature extractor involving randomness:

Compare the results:

Use different values for the RandomSeeding option to compute the distance matrices:

Compare the results:

WorkingPrecision  (1)

Compute the distance matrix for 500 random numerical vectors of length 100 that have a precision of 30:

DistanceMatrix uses arbitrary-precision computation:

Using WorkingPrecisionMachinePrecision can speed up the computation:

But the results are not as precise:

When vectors are similar, changing the value of WorkingPrecision can lead to significantly different results:

Applications  (1)

Find the minimum distance between two sets of points:

Wolfram Research (2015), DistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/DistanceMatrix.html (updated 2017).

Text

Wolfram Research (2015), DistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/DistanceMatrix.html (updated 2017).

CMS

Wolfram Language. 2015. "DistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/DistanceMatrix.html.

APA

Wolfram Language. (2015). DistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DistanceMatrix.html

BibTeX

@misc{reference.wolfram_2023_distancematrix, author="Wolfram Research", title="{DistanceMatrix}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/DistanceMatrix.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_distancematrix, organization={Wolfram Research}, title={DistanceMatrix}, year={2017}, url={https://reference.wolfram.com/language/ref/DistanceMatrix.html}, note=[Accessed: 19-March-2024 ]}