vec2sym: Convert vector to symmetric matrix

View source: R/utils.R

vec2symR Documentation

Convert vector to symmetric matrix

Description

Reverse operation of upper.tri or lower.tri. Given a vector of upper or lower triangular elements of a matrix (optionally including the diagonal elements), returns the corresponding symmetric matrix. The elements of the vector can be arranged by column (the default) or by row.

Usage

vec2sym(x, lower = TRUE, byrow = FALSE)

Arguments

x

vector containing upper or lower triangular elements of a symmetric matrix.

lower

x is from the lower triangle (default = TRUE)

byrow

the elements in x are ordered row-wise (default = FALSE)

Details

Many thanks for the author of the patr1ckm package for providing us this method, Handy R functions, enabling to revert an upper.tri or lower.tri operation.

Note that if x is a vector of the lower triagular elements given by column, this is equivalent to the upper triangular elements given by row. Similarly, if x is a vector of the lower triangular elements given by row, this is equivalent to the upper triangular elements given by column.

Value

Symmetric matrix

Author(s)

patr1ckm

Examples

x <- c(1, 1, 1, 2, 2, 3)
check <- matrix(c(1, 1, 1, 1, 2, 2, 1, 2, 3), 3, 3)
identical(vec2sym(x, lower = TRUE, byrow = FALSE), check)

bastienchassagnol/RGMMBench documentation built on Oct. 26, 2023, 5:58 p.m.