Language Reference
DISTANCE Function
DISTANCE (x, <, method> ) ;
DISTANCE (x, y, <, method> ) ;
This function is supported by the IML procedure and the iml action.
The DISTANCE function computes the pairwise distances in one of two ways:
When called with one numerical matrix, x, the DISTANCE function computes the pairwise distances between the rows of x.
When called with two numerical matrices, x and y, the DISTANCE function computes the pairwise distances between rows of x and the rows of y.
The arguments are as follows:
- x
specifies an numerical matrix that contains n points in d-dimensional space.
- y
specifies an numerical matrix that contains m points in d-dimensional space.
- method
is an optional argument that specifies which method to use to specify the distance between pairs of points. The method argument is either a scalar numeric value or a case-insensitive character value. Only the first four characters are used to distinguish one method from the others. You can specify the following values for method:
- "L2"
computes the Euclidean () distance between two points. This is the default value. An equivalent alias is "Euclidean".
- "L1"
computes the Manhattan () distance between two points. An equivalent alias is "CityBlock" or "Manhattan".
- "LInf"
computes the Chebyshev () distance between two points. An equivalent alias is "Chebyshev".
- p
Distance between Rows of a Matrix
When you specify a single numerical matrix x, the DISTANCE function returns an symmetric matrix. The (i, j) element is the distance between the ith and jth rows of x.
If u and v are two d-dimensional points, then the following formulas are used to compute the distance between u and v:
The following statements illustrate the DISTANCE function:
x = {1 0,
0 1,
-1 0,
0 -1};
d2 = distance(x, "L2");
print d2[format=best5.];
Figure 125: Euclidean Distance between Pairs of Points
| d2 | |||
|---|---|---|---|
| 0 | 1.414 | 2 | 1.414 |
| 1.414 | 0 | 1.414 | 2 |
| 2 | 1.414 | 0 | 1.414 |
| 1.414 | 2 | 1.414 | 0 |
The ith column of d2 contains the distances between the ith row of x and the other rows. Notice that the d2 matrix has zeros along the diagonal.
You can also compute non-Euclidean distances, as follows:
d1 = distance(x, "L1");
dInf = distance(x, "LInfinity");
print d1, dInf;
Figure 126: Distance between Pairs of Points
| d1 | |||
|---|---|---|---|
| 0 | 2 | 2 | 2 |
| 2 | 0 | 2 | 2 |
| 2 | 2 | 0 | 2 |
| 2 | 2 | 2 | 0 |
| dInf | |||
|---|---|---|---|
| 0 | 1 | 2 | 1 |
| 1 | 0 | 1 | 2 |
| 2 | 1 | 0 | 1 |
| 1 | 2 | 1 | 0 |
If a row contains a missing value, all distances that involve that row are assigned a missing value.
Distance between Rows of Two Matrices
You can also specify two matrices as arguments. If x is an numerical matrix and y is an numerical matrix, the DISTANCE function returns an matrix. The (i, j) element is the distance between the ith row of x and the jth row of y. The following example computes the pairwise distances between four points in x and two points in y. The distances are shown in Figure 127.
x = {1 0,
0 1,
-1 0,
0 -1};
y = {0 0,
1 1};
dxy = distance(x, y);
print dxy[format=best5.];
Figure 127: Pairwise Euclidean Distance between Rows of Matrices
| dxy | |
|---|---|
| 1 | 1 |
| 1 | 1 |
| 1 | 2.236 |
| 1 | 2.236 |
The jth column of dxy contains the distances between the jth row of y and the rows of x. For example, the first column in Figure 127 shows that the first row of y (which is (0, 0)) is one unit away from every ordered pair in the rows of x. The second column shows that the second row of y (which is (1, 1)) is one unit away from the coordinates in the first two rows of x, but is 2.236 units away from the coordinates in the last two rows of x.