# Create a symmetric matrix
m = matrix(c(1:16), 4,4)
mat = (m + t(m));
diag(mat) = 0;
# Print the matrix
mat
# Take the lower triangle and vectorize it (in two ways)
x1 = mat[lower.tri(mat)]
x2 = as.vector(as.dist(mat))
all.equal(x1, x2) # The vectors are equal
# Turn the vectors back into matrices
new.mat = lowerTri2matrix(x1, diag = 0);
# Did we get back the same matrix?
all.equal(mat, new.mat)
Run the code above in your browser using DataLab