smtools {corpcor} | R Documentation |
Some Tools for Handling Symmetric Matrices
Description
sm2vec
takes a symmetric matrix and puts
the lower triagonal entries into a vector (cf. lower.tri
).
sm.index
lists the corresponding x-y-indices for each entry
in the vector produced by sm2vec
.
vec2sm
reverses the operation by sm2vec
and converts the
vector back to a symmetric matrix. If diag=FALSE
the
diagonal of the resulting matrix will consist of NAs. If order
is supplied then the input vector vec
will first be rearranged accordingly.
Usage
sm2vec(m, diag = FALSE)
sm.index(m, diag = FALSE)
vec2sm(vec, diag = FALSE, order = NULL)
Arguments
m |
symmetric matrix |
diag |
logical. Should the diagonal be included in the conversion to and from a vector? |
vec |
vector of unique elements from a symmetric matrix |
order |
order of the entries in |
Value
A vector (sm2vec
), a two-column matrix with indices (sm.index
),
or a symmetric matrix (vec2sm
).
Author(s)
Korbinian Strimmer (https://strimmerlab.github.io/).
See Also
Examples
# load corpcor library
library("corpcor")
# a symmetric matrix
m = rbind(
c(3,1,1,0),
c(1,3,0,1),
c(1,0,2,0),
c(0,1,0,2)
)
m
# convert into vector (including the diagonals)
v = sm2vec(m, diag=TRUE)
v.idx = sm.index(m, diag=TRUE)
v
v.idx
# put back to symmetric matrix
vec2sm(v, diag=TRUE)
# convert from vector with specified order of the elements
sv = sort(v)
sv
ov = order(v)
ov
vec2sm(sv, diag=TRUE, order=ov)