Scale the Rows and Columns of a Matrix
dimScale
, rowScale
, and colScale
implement D1 %*% x %*% D2
, D %*% x
, and x %*% D
for diagonal matrices D1
, D2
, and D
with diagonal entries d1
, d2
, and d
, respectively. Unlike the explicit products, these functions preserve dimnames(x)
and symmetry where appropriate.
dimScale(x, d1 = sqrt(1/diag(x, names = FALSE)), d2 = d1) rowScale(x, d) colScale(x, d)
x
: a matrix, possibly inheriting from virtual class Matrix
.d1,d2,d
: numeric vectors giving factors by which to scale the rows or columns of x
; they are recycled as necessary.dimScale(x)
(with d1
and d2
unset) is only roughly equivalent to cov2cor(x)
. cov2cor
sets the diagonal entries of the result to 1 (exactly); dimScale
does not.
The result of scaling x
, currently always inheriting from virtual class dMatrix
.
It inherits from triangularMatrix
if and only if x
does. In the special case of dimScale(x, d1, d2)
with identical d1
and d2
, it inherits from symmetricMatrix
if and only if x
does.
Mikael Jagan
cov2cor
n <- 6L (x <- forceSymmetric(matrix(1, n, n))) dimnames(x) <- rep.int(list(letters[seq_len(n)]), 2L) d <- seq_len(n) (D <- Diagonal(x = d)) (scx <- dimScale(x, d)) # symmetry and 'dimnames' kept (mmx <- D %*% x %*% D) # symmetry and 'dimnames' lost stopifnot(identical(unname(as(scx, "generalMatrix")), mmx)) rowScale(x, d) colScale(x, d)
Useful links
Downloads (last 30 days):