# NAG FL Interfaceg03ebf (distance_​mat_​2)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g03ebf computes a distance (dissimilarity) matrix between two sets of observations.

## 2Specification

Fortran Interface
 Subroutine g03ebf ( scal, p, m, n, l, x, ldx, isv, y, ldy, sx, sy, d,
 Integer, Intent (In) :: m, n, l, ldx, isv(l), ldy Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: p, x(ldx,l), y(ldy,l) Real (Kind=nag_wp), Intent (Inout) :: sx(l), sy(*), d(m,n) Character (1), Intent (In) :: update, scal, stype
#include <nag.h>
 void g03ebf_ (const char *update, const char *scal, const char *stype, const double *p, const Integer *m, const Integer *n, const Integer *l, const double x[], const Integer *ldx, const Integer isv[], const double y[], const Integer *ldy, double sx[], double sy[], double d[], Integer *ifail, const Charlen length_update, const Charlen length_scal, const Charlen length_stype)
The routine may be called by the names g03ebf or nagf_mv_distance_mat_2.

## 3Description

Given two sets of observations on $l$ variables, a distance matrix is such that the $ij$th element represents how far apart or how dissimilar the $i$th observation from the first set and the $j$th observation from the second set are.
Let $X$ and $Y$ be $m×l$ and $n×l$ data matrices of $m$ and $n$ observations, respectively, on $l$ variables. The distance between observation $i$ from $X$ and observation $j$ from $Y$, ${d}_{ij}$, is most commonly defined in terms of the scaled Minkowski $p$-norm:
 $dij= { ∑k=1p (|xik/sxi-yjk/syj|) p } 1/p ,$
where ${x}_{ik}$ and ${y}_{jk}$ are the $ik$th and $jk$th elements of $X$ and $Y$ respectively, ${\mathrm{sx}}_{i}$ is a standardization for the $i$th variable in $X$, ${\mathrm{sy}}_{j}$ is a standardization for the $j$th variable in $Y$, and $p$ is the order of the Minkowski norm.
Three standardizations (scalings) for the variables are available.
1. (a)Standard deviation: ${\mathrm{sx}}_{i}=\sqrt{\sum _{k=1}^{n}{\left({x}_{ji}-\overline{x}\right)}^{2}/\left(n-1\right)}$
2. (b)Range: ${\mathrm{sx}}_{i}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)-\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)$
3. (c)User-supplied values of ${\mathrm{sx}}_{i}$.
In addition to the Minkowski measure 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 g03ebf 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.
g03ebf returns the full rectangular distance matrix.

## 4References

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

## 5Arguments

1: $\mathbf{update}$Character(1) Input
On entry: indicates whether or not an existing matrix is to be updated.
${\mathbf{update}}=\text{'U'}$
The matrix $D$ is updated and distances are added to $D$.
${\mathbf{update}}=\text{'I'}$
The matrix $D$ is initialized to zero before the distances are added to $D$.
Constraint: ${\mathbf{update}}=\text{'U'}$ or $\text{'I'}$.
2: $\mathbf{scal}$Character(1) Input
On entry: indicates the standardization of the variables to be used.
${\mathbf{scal}}=\text{'S'}$
Standard deviation.
${\mathbf{scal}}=\text{'R'}$
Range.
${\mathbf{scal}}=\text{'G'}$
Standardizations given in array sx (and posibly sy).
${\mathbf{scal}}=\text{'U'}$
Unscaled.
Constraint: ${\mathbf{scal}}=\text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
3: $\mathbf{stype}$Character(1) Input
On entry: indicates how the standardization of the variables treats the two sets of observations.
${\mathbf{stype}}=\text{'A'}$
Amalgamated.
${\mathbf{stype}}=\text{'I'}$
Independent.
${\mathbf{stype}}=\text{'X'}$
Standardization is based purley on observations in x.
Constraint: ${\mathbf{stype}}=\text{'A'}$, $\text{'I'}$ or $\text{'X'}$.
4: $\mathbf{p}$Real (Kind=nag_wp) Input
On entry: the order $p$ of the Minkowski distance metric.
Constraint: ${\mathbf{p}}\ge 1.0$.
5: $\mathbf{m}$Integer Input
On entry: $m$, the number of observations in the data array x.
Constraint: ${\mathbf{m}}\ge 1$.
6: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations in the data array y.
Constraint: ${\mathbf{n}}\ge 1$.
7: $\mathbf{l}$Integer Input
On entry: l, the total number of variables in arrays x and y.
Constraint: ${\mathbf{l}}>0$.
8: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{l}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{x}}\left(\mathit{i},\mathit{k}\right)$ must contain the value of the $\mathit{k}$th variable for the $\mathit{i}$th observation in the first set of observations, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
9: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g03ebf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{m}}$.
10: $\mathbf{isv}\left({\mathbf{l}}\right)$Integer array Input
On entry: ${\mathbf{isv}}\left(j\right)$ indicates whether or not the $j$th variable in x and y is to be included in the distance computations.
If ${\mathbf{isv}}\left(\mathit{k}\right)=0$ the $\mathit{j}$th variable is not included., for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
If ${\mathbf{isv}}\left(\mathit{k}\right)\ge 1$ the $\mathit{j}$th variable is included, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$
Constraint: ${\mathbf{isv}}\left(\mathit{j}\right)>0$ for at least one $\mathit{j}$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
11: $\mathbf{y}\left({\mathbf{ldy}},{\mathbf{l}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{y}}\left(\mathit{j},\mathit{k}\right)$ must contain the value of the $\mathit{k}$th variable for the $\mathit{j}$th observation in the second set of observations, for $\mathit{j}=1,2,\dots ,n$ and $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
12: $\mathbf{ldy}$Integer Input
On entry: the first dimension of the array y as declared in the (sub)program from which g03ebf is called.
Constraint: ${\mathbf{ldy}}\ge {\mathbf{n}}$.
13: $\mathbf{sx}\left({\mathbf{l}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{isv}}\left(\mathit{k}\right)>0$ then ${\mathbf{sx}}\left(\mathit{k}\right)$ must contain the scaling for variable $\mathit{k}$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
Constraint: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{isv}}\left(k\right)>0$, ${\mathbf{sx}}\left(\mathit{k}\right)>0.0$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
On exit: if ${\mathbf{scal}}=\text{'S'}$ and ${\mathbf{isv}}\left(k\right)>0$ then ${\mathbf{sx}}\left(k\right)$ contains the standard deviation of the variable in the $k$th column of x.
If ${\mathbf{scal}}=\text{'R'}$ and ${\mathbf{isv}}\left(k\right)>0$, ${\mathbf{sx}}\left(k\right)$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\text{'U'}$ and ${\mathbf{isv}}\left(k\right)>0$, ${\mathbf{sx}}\left(k\right)=1.0$.
If ${\mathbf{scal}}=\text{'G'}$, sx is unchanged.
14: $\mathbf{sy}\left(*\right)$Real (Kind=nag_wp) array Input/Output
Note: the dimension of the array sy must be at least ${\mathbf{l}}$ if ${\mathbf{stype}}=\text{'I'}$ and ${\mathbf{scal}}\ne \text{'U'}$, and at least $0$ otherwise.
On entry: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{stype}}=\text{'I'}$ and ${\mathbf{isv}}\left(\mathit{k}\right)>0$ then ${\mathbf{sy}}\left(\mathit{k}\right)$ must contain the scaling for variable $\mathit{k}$ , for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
If ${\mathbf{stype}}\ne \text{'I'}$, or ${\mathbf{scal}}=\text{'U'}$ then sy is not referenced and need not be set.
Constraint: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{stype}}=\text{'I'}$ and ${\mathbf{isv}}\left(k\right)>0$, ${\mathbf{sy}}\left(\mathit{k}\right)>0.0$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
On exit: if ${\mathbf{scal}}=\text{'S'}$ and ${\mathbf{stype}}=\text{'I'}$ and ${\mathbf{isv}}\left(k\right)>0$ then ${\mathbf{sy}}\left(k\right)$ contains the standard deviation of the variable in the $k$th column of x.
If ${\mathbf{scal}}=\text{'R'}$ and ${\mathbf{stype}}=\text{'I'}$ and ${\mathbf{isv}}\left(k\right)>0$, ${\mathbf{sy}}\left(k\right)$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\text{'U'}$, sy is unchanged.
If ${\mathbf{scal}}=\text{'G'}$, sy is unchanged.
15: $\mathbf{d}\left({\mathbf{m}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: the $m×n$ distance matrix $D$.
If ${\mathbf{update}}=\text{'U'}$, d need not be set.
On exit: the (possibly updated) distance matrix $D$.
16: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{update}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{update}}=\text{'U'}$ or $\text{'I'}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{scal}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{scal}}=\text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{stype}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{stype}}=\text{'A'}$, $\text{'I'}$ or $\text{'X'}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{p}}\ge 1.0$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 1$.
${\mathbf{ifail}}=8$
Variable $⟨\mathit{\text{value}}⟩$ is constant.
${\mathbf{ifail}}=9$
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=10$
On entry, isv does not contain a positive element.
${\mathbf{ifail}}=12$
On entry, ${\mathbf{ldy}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldy}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=13$
On entry, at least one element of ${\mathbf{sx}}\le 0.0$.
On entry, at least one element of ${\mathbf{sx}}\le 0.0$ or ${\mathbf{sy}}\le 0.0$.
${\mathbf{ifail}}=15$
On entry, at least one element of ${\mathbf{d}}<0.0$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g03ebf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

When there is a large number of observations and these do not require scaling factors to be computed internally, then the distance matrix for all observations can be computed in stages by using g03ebf to systematically calculate the distance matrix for pairs of row blocks of observations. When there are a large number of variables, the distance matrix can be updated in stages using observations on blocks of variables at each update.

## 10Example

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

### 10.1Program Text

Program Text (g03ebfe.f90)

### 10.2Program Data

Program Data (g03ebfe.d)

### 10.3Program Results

Program Results (g03ebfe.r)