Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Format distance matrix

`ZOut = squareform(yIn)`

`yOut = squareform(ZIn)`

`ZOut = squareform(yIn,'tomatrix')`

`yOut = squareform(ZIn,'tovector')`

converts `ZOut`

= squareform(`yIn`

)`yIn`

, a pairwise distance vector of length
*m*(*m*–1)/2 for *m*
observations, into `ZOut`

, an
*m*-by-*m* symmetric matrix with zeros
along the diagonal.

The pairwise distances in `yIn`

are arranged in the order
(2,1), (3,1), ..., (*m*,1), (3,2), ...,
(*m*,2), ..., (*m*,*m*–1).
The pairwise distance between the `i`

th and
`j`

th observations is in `ZOut(i,j)`

and
`yIn((i–1)*(m–i/2)+j–i)`

for *i*≤*j*.

forces `yOut`

= squareform(`ZIn`

,'tovector')`squareform`

to treat `ZIn`

as a
matrix and converts `ZIn`

into a vector. If
`ZIn`

is a scalar (1-by-1), then `ZIn`

must be zero.

The previous two syntaxes are useful when the input argument is a scalar. If
you do not specify either `'tomatrix'`

or
`'tovector'`

, then the default is
`'tomatrix'`

.

You can use

`squareform`

to format a vector or matrix that is similar to a distance vector or matrix, such as the correlation coefficient matrix (`corrcoef`

).