squareform — Convert between condensed distance vectors and square symmetric matrices.
squareform converts a condensed pairwise-distance vector into a square symmetric matrix, or converts a square symmetric distance matrix back into condensed form.
Syntax
Y = squareform(Z)
Y = squareform(Z, Force)Inputs
Z- Type
Any- Required
- Yes
- Description
- Condensed distance vector or square distance matrix.
force- Type
StringScalar- Required
- No
- Description
- Conversion direction: 'tomatrix' or 'tovector'.
Returns
Y- Type
Any- Description
- Square symmetric matrix or condensed distance vector, preserving the input class.
Errors
RunMat:distance:InvalidArgument- When
- Distance inputs, dimensions, metrics, or controls are malformed.
- Message
distance helper: invalid argument
RunMat:distance:Internal- When
- RunMat cannot allocate or construct a distance output.
- Message
distance helper: internal error
Behavior
Vector inputs must have length n*(n-1)/2 for an integer n. Condensed vectors use the same pair ordering as pdist.
Matrix inputs must be square and symmetric with a zero diagonal. Floating-point symmetry and zero tests use RunMat's distance tolerance; integer and logical comparisons remain exact.
squareform(Z,"tomatrix") forces vector-to-matrix conversion; squareform(Z,"tovector") forces matrix-to-vector conversion. Without a direction, vectors expand and nonvector matrices condense.
The conversion preserves single, double, and logical classes. RunMat mode also preserves all eight typed-integer classes as an extension. Structural integer values do not pass through double.
Execution
Automatically resident inputs may gather to host for structural conversion. An explicit gpuArray input is a RunMat-only extension. squareform has no GPU kernel or fused path.
The entire output must fit host memory. Shape and option errors are checked before output construction.
Examples
Expand a condensed vector
D = [5 4 7]; M = squareform(D)Expected output:
M = [0 5 4; 5 0 7; 4 7 0]Convert back to condensed form
M = [0 5 4; 5 0 7; 4 7 0]; D2 = squareform(M)Expected output:
D2 = [5 4 7]Force vector-to-matrix conversion
D = [5 4 7]; M = squareform(D,"tomatrix")Expected output:
M is the 3-by-3 symmetric distance matrix.Preserve logical distance storage
D = logical([1 0 1]); M = squareform(D); V = squareform(M)Expected output:
Both M and V have logical class; V equals D.Using squareform with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how squareform changes the result.
Run a small squareform example, explain the result, then change one input and compare the output.
FAQ
Why does a matrix input need a zero diagonal?⌄
A distance matrix represents distance from each observation to itself on the diagonal, which must be zero for condensed conversion.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · nlparci · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · test · training · tsne
Summary
anova1 · binocdf · binscatter · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · ksdensity · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pca · pdf · prctile · qqplot · quantile · refline · rmse · scatterhist · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · gpurng · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.