NAG C Library Function Document

nag_mv_distance_mat (g03eac)


nag_mv_distance_mat (g03eac) computes a distance (dissimilarity) matrix.


#include <nag.h>
#include <nagg03.h>
void  nag_mv_distance_mat (Nag_MatUpdate update, Nag_DistanceType dist, Nag_VarScaleType scale, Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], double s[], double d[], NagError *fail)


Given n  objects, a distance or dissimilarity matrix, is a symmetric matrix with zero diagonal elements such that the ij th element represents how far apart or how dissimilar the i th and j th objects are.
Let X  be an n  by p  data matrix of observations of p  variables on n  objects, then the distance between object j  and object k , d jk , can be defined as:
d jk = i=1 p D x ji / s i , x ki / s i α ,  
where x ji  and x ki  are the j,i th and k,i th elements of X , s i  is a standardization for the i th variable and D u,v  is a suitable function. Three functions are provided in nag_mv_distance_mat (g03eac):
(a) Euclidean distance: D u,v = u-v 2  and α = 1 2 .
(b) Euclidean squared distance: D u,v = u-v 2  and α=1 .
(c) Absolute distance (city block metric): D u,v = u-v  and α=1 .
Three standardizations are available:
1. Standard deviation: s i = j=1 n x ji - x - 2 / n-1
2. Range: s i = max x 1i , x 2i , , x ni - min x 1i , x 2i , , x ni
3. User-supplied values of s i .
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 (g03eac) 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 the g02 Chapter Introduction and the g11 Chapter Introduction respectively.
nag_mv_distance_mat (g03eac) returns the strictly lower triangle of the distance matrix.


Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press


1:     update Nag_MatUpdateInput
On entry: indicates whether or not an existing matrix is to be updated.
The matrix D  is updated and distances are added to D .
The matrix D  is initialized to zero before the distances are added to D .
Constraint: update=Nag_MatUp or Nag_NoMatUp.
2:     dist Nag_DistanceTypeInput
On entry: indicates which type of distances are computed.
Absolute distances.
Euclidean distances.
Euclidean squared distances.
Constraint: dist=Nag_DistAbs, Nag_DistEuclid or Nag_DistSquared.
3:     scale Nag_VarScaleTypeInput
On entry: indicates the standardization of the variables to be used.
Standard deviation.
Standardizations given in array S.
Constraint: scale=Nag_VarScaleStd, Nag_VarScaleRange, Nag_VarScaleUser or Nag_NoVarScale.
4:     n IntegerInput
On entry: n, the number of observations.
Constraint: n2 .
5:     m IntegerInput
On entry: the total number of variables in array x.
Constraint: m>0 .
6:     x[n×tdx] const doubleInput
On entry: x[i-1×tdx+j-1]  must contain the value of the j th variable for the i th object, for i=1,2,,n and j=1,2,,m.
7:     tdx IntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxm .
8:     isx[m] const IntegerInput
On entry: isx[j-1]  indicates whether or not the j th variable in x is to be included in the distance computations.
If isx[j-1] > 0  the j th variable is included, for j=1,2,,m; otherwise it is not referenced.
Constraint: isx[j-1] > 0  for at least one j , for j=1,2,,m.
9:     s[m] doubleInput/Output
On entry: if scale=Nag_VarScaleUser and isx[j-1] > 0  then s[j-1]  must contain the scaling for variable j , for j=1,2,,m.
Constraint: if scale=Nag_VarScaleUser and isx[j-1] > 0 , s[j-1] > 0.0 , for j=1,2,,m.
On exit: if scale=Nag_VarScaleStd and isx[j-1] > 0  then s[j-1]  contains the standard deviation of the variable in the j th column of x.
If scale=Nag_VarScaleRange and isx[j-1] > 0  then s[j-1]  contains the range of the variable in the j th column of x.
If scale=Nag_NoVarScale and isx[j-1] > 0  then s[j-1] = 1.0  and if scale=Nag_VarScaleUser then s is unchanged.
10:   d[n×n-1/2] doubleInput/Output
On entry: if update=Nag_MatUp then d must contain the strictly lower triangle of the distance matrix D  to be updated. D  must be stored packed by rows, i.e., d[ i-1 i-2 / 2 + j - 1 ] , i>j  must contain d ij .
Constraint: if update=Nag_MatUp, d[j-1] 0.0 , for j=1,2,, n n-1 / 2 .
On exit: the strictly lower triangle of the distance matrix D  stored packed by rows, i.e., d ij  is contained in d[ i-1 i-2 / 2 + j - 1 ] , i>j .
11:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

On entry, tdx=value  while m=value . These arguments must satisfy tdxm .
On entry, argument dist had an illegal value.
On entry, argument scale had an illegal value.
On entry, argument update had an illegal value.
On entry, scale=Nag_VarScaleRange or scale=Nag_VarScaleStd, and x[i-1×tdx+j-1] = x[i×tdx+j-1] , for i=1,2,,n - 1, for some j  with isx[i-1] > 0 .
On entry, m=value.
Constraint: m>0.
On entry, n=value.
Constraint: n2.
On entry, isx[value] = value.
Constraint: isx[i-1] > 0 , for at least one i , i = 1 , 2 , , m .
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
On entry, d[value] = value.
Constraint: d[i-1] 0 , for i=1,2,, n × n-1 / 2, when update=Nag_MatUp.
On entry, s[value] = value.
Constraint: s[j-1] > 0 , for j=1,2,,m, when scale=Nag_VarScaleUser and isx[j-1] > 0 .


The computations are believed to be stable.

Parallelism and Performance

nag_mv_distance_mat (g03eac) is not threaded in any implementation.

Further Comments

nag_mv_hierar_cluster_analysis (g03ecc) can be used to perform cluster analysis on the computed distance matrix.


A data matrix of five observations and three variables is read in and a distance matrix is calculated from variables 2 and 3 using squared Euclidean distance with no scaling. This matrix is then printed.

Program Text

Program Text (g03eace.c)

Program Data

Program Data (g03eace.d)

Program Results

Program Results (g03eace.r)