Learn R Programming

corpcor (version 1.1.2)

smtools: Some Tools for Symmetric Matrices

Description

sm2vec takes a symmetric matrix and puts the lower triagonal entries into a vector (cf. lower.tri).

sm.indexes gives the corresponding x-y-indexes for each entry in the vector produced by sm2vec. vec2sm reverses the operation by sm2vec and turns the vector back in a symmetric matrix. Note that if diag=FALSE the diagonal of the resulting matrix will consist of NAs. If order is given then the input vector vec is first sorted accordingly.

Usage

sm2vec(m, diag = FALSE)
sm.indexes(m, diag = FALSE)
vec2sm(vec, diag = FALSE, order = NULL)

Arguments

m
symmetric matrix
diag
logical. Should the diagonal be included?
vec
vector of unique elements from a symmetric matrix
order
order of the entries in vec

Value

  • A vector (sm2vec), a two-column matrix with indexes (sm.indexes), or a symmetric matrix (vec2sm).

See Also

lower.tri.

Examples

Run this code
# load corpcor library
library("corpcor")

# covariance matrix
m.cov <- rbind(
 c(3,1,1,0),
 c(1,3,0,1),
 c(1,0,2,0),
 c(0,1,0,2)
)
m.cov

# convert into vector (including diagonals
v <- sm2vec(m.cov, diag=TRUE)
v.idx <- sm.indexes(m.cov, diag=TRUE)
v
v.idx

# put back to symmetric matrix
vec2sm(v, diag=TRUE)

# vector not in the original order
sv <- sort(v)
sv
ov <- order(v)
ov
vec2sm(sv, diag=TRUE, order=ov)

Run the code above in your browser using DataLab