Format distance matrix
Z = squareform(y)
y = squareform(Z)
Z = squareform(y,'tovector')
Y = squareform(Z,'tomatrix')
Z = squareform(y), where y is a vector as created by the pdist function, converts y into a square, symmetric format Z, in which Z(i,j) denotes the distance between the ith and jth objects in the original data.
y = squareform(Z), where Z is a square, symmetric matrix with zeros along the diagonal, creates a vector y containing the Z elements below the diagonal. y has the same format as the output from the pdist function.
Z = squareform(y,'tovector') forces squareform to treat y as a vector.
Y = squareform(Z,'tomatrix') forces squareform to treat Z as a matrix.
The last two formats are useful if the input has a single element, so that it is ambiguous whether the input is a vector or square matrix.
y = 1:6 y = 1 2 3 4 5 6 X = [0 1 2 3; 1 0 4 5; 2 4 0 6; 3 5 6 0] X = 0 1 2 3 1 0 4 5 2 4 0 6 3 5 6 0
Then squareform(y) = X and squareform(X) = y.