Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

lowerTri2matrix

Reconstruct a symmetric matrix from a distance (lower-triangular) representation


Description

Assuming the input vector contains a vectorized form of the distance representation of a symmetric matrix, this function creates the corresponding matrix. This is useful when re-forming symmetric matrices that have been vectorized to save storage space.

Usage

lowerTri2matrix(x, diag = 1)

Arguments

x

a numeric vector

diag

value to be put on the diagonal. Recycled if necessary.

Details

The function assumes that x contains the vectorized form of the distance representation of a symmetric matrix. In particular, x must have a length that can be expressed as n*(n-1)/2, with n an integer. The result of the function is then an n times n matrix.

Value

A symmetric matrix whose lower triangle is given by x.

Author(s)

Peter Langfelder

Examples

# 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)

WGCNA

Weighted Correlation Network Analysis

v1.70-3
GPL (>= 2)
Authors
Peter Langfelder <Peter.Langfelder@gmail.com> and Steve Horvath <SHorvath@mednet.ucla.edu> with contributions by Chaochao Cai, Jun Dong, Jeremy Miller, Lin Song, Andy Yip, and Bin Zhang
Initial release
2021-02-17

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.