PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_mv_distance_mat (g03ea)
Purpose
nag_mv_distance_mat (g03ea) computes a distance (dissimilarity) matrix.
Syntax
[
s,
d,
ifail] = g03ea(
update,
dist,
scal,
x,
isx,
s,
d, 'n',
n, 'm',
m)
[
s,
d,
ifail] = nag_mv_distance_mat(
update,
dist,
scal,
x,
isx,
s,
d, 'n',
n, 'm',
m)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: |
n was made optional |
Description
Given objects, a distance or dissimilarity matrix is a symmetric matrix with zero diagonal elements such that the th element represents how far apart or how dissimilar the th and th objects are.
Let
be an
by
data matrix of observations of
variables on
objects, then the distance between object
and object
,
, can be defined as:
where
and
are the
th and
th elements of
,
is a standardization for the
th variable and
is a suitable function. Three functions are provided in
nag_mv_distance_mat (g03ea).
(a) |
Euclidean distance: and . |
(b) |
Euclidean squared distance: and . |
(c) |
Absolute distance (city block metric):
and . |
Three standardizations are available.
(a) |
Standard deviation: |
(b) |
Range: |
(c) |
User-supplied values of . |
In addition to the above distances there are a large number of other dissimilarity measures, particularly for dichotomous variables (see
Krzanowski (1990) and
Everitt (1974)). For the dichotomous case these measures are simple to compute and can, if suitable scaling is used, be combined with the distances computed by
nag_mv_distance_mat (g03ea) using the updating option.
Dissimilarity measures for variables can be based on the correlation coefficient for continuous variables and contingency table statistics for dichotomous data, see chapters G02 and G11 respectively.
nag_mv_distance_mat (g03ea) returns the strictly lower triangle of the distance matrix.
References
Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press
Parameters
Compulsory Input Parameters
- 1:
– string (length ≥ 1)
-
Indicates whether or not an existing matrix is to be updated.
- The matrix is updated and distances are added to .
- The matrix is initialized to zero before the distances are added to .
Constraint:
or .
- 2:
– string (length ≥ 1)
-
Indicates which type of distances are computed.
- Absolute distances.
- Euclidean distances.
- Euclidean squared distances.
Constraint:
, or .
- 3:
– string (length ≥ 1)
-
Indicates the standardization of the variables to be used.
- Standard deviation.
- Range.
- Standardizations given in array s.
- Unscaled.
Constraint:
, , or .
- 4:
– double array
-
ldx, the first dimension of the array, must satisfy the constraint
.
must contain the value of the th variable for the th object, for and .
- 5:
– int64int32nag_int array
-
indicates whether or not the
th variable in
x is to be included in the distance computations.
If
the th variable is included, for ; otherwise it is not referenced.
Constraint:
for at least one , for .
- 6:
– double array
-
If and
then must contain the scaling for variable , for .
Constraint:
if and , , for .
- 7:
– double array
-
If
,
d must contain the strictly lower triangle of the distance matrix
to be updated.
must be stored packed by rows, i.e.,
,
must contain
.
If
,
d need not be set.
Constraint:
if , , for .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the first dimension of the array
x.
, the number of observations.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
isx,
s and the second dimension of the array
x. (An error is raised if these dimensions are not equal.)
The total number of variables in array
x.
Constraint:
.
Output Parameters
- 1:
– double array
-
If
and
then
contains the standard deviation of the variable in the
th column of
x.
If
and
,
contains the range of the variable in the
th column of
x.
If and , .
If
,
s is unchanged.
- 2:
– double array
-
The strictly lower triangle of the distance matrix stored packed by rows, i.e., is contained in , .
- 3:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | , |
or | , |
or | or , |
or | , or , |
or | , , or . |
-
-
On entry, | , for , |
or | and , for some , |
or | or and for , for some with . |
or | for some when and . |
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
The computations are believed to be stable.
Further Comments
nag_mv_cluster_hier (g03ec) can be used to perform cluster analysis on the computed distance matrix.
Example
A data matrix of five observations and three variables is read in and a distance matrix is calculated from variables and using squared Euclidean distance with no scaling. This matrix is then printed.
Open in the MATLAB editor:
g03ea_example
function g03ea_example
fprintf('g03ea example results\n\n');
x = [1, 1, 1;
2, 1, 2;
3, 6, 3;
4, 8, 2;
5, 8, 0];
[n,m] = size(x);
isx = ones(m,1,'int64');
isx(1) = int64(0);
s = ones(m,1);
ld = (n*(n-1))/2;
d = zeros(ld,1);
update = 'I';
dist = 'S';
scal = 'U';
[s, d, ifail] = g03ea( ...
update, dist, scal, x, isx, s, d);
fprintf(' Distance Matrix\n ');
fprintf(' %5d', [1:n-1]);
for i = 2:n
lj = (i-1)*(i-2)/2 + 1;
uj = i*(i-1)/2;
fprintf('\n%2d ', i);
fprintf(' %5.2f', d(lj:uj));
end
fprintf('\n');
g03ea example results
Distance Matrix
1 2 3 4
2 1.00
3 29.00 26.00
4 50.00 49.00 5.00
5 50.00 53.00 13.00 4.00
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015