Efficiently normalize the row or column vectors of a dense or sparse matrix to unit length.
normalize.rows(M, method = "euclidean", p = 2, ...,
tol = 1e-6, inplace = FALSE)normalize.cols(M, method = "euclidean", p = 2, ...,
tol = 1e-6, inplace = FALSE)
A row-normalized (or column-normalized) matrix with the same dimensions as \(M\).
a dense or sparse numeric matrix
norm to be computed, see rowNorms
exponent of Minkowski p-norm in the range \(0 < p \le \infty\). Note that normalization is not possible for very small values of \(p\).
any further arguments are passed to rowNorms
(or colNorms
)
row/column vectors with norm below tol
are assumed to be all zeroes and cannot be normalized (see “Details” below)
if TRUE
, modify the matrix M
in place. Don't ever set this argument to TRUE
.
Stephanie Evert (https://purl.org/stephanie.evert)
These functions return a matrix with row (or column) vectors rescaled to a length of 1 according to the selected norm.
All-zero vectors (with \(\|0\| = 0\)) cannot be normalized. In order to avoid scaling up rounding errors, rows (or columns) with \(\|x\| < tol\) are explicitly set to 0 (and thus not normalized). Since a suitable threshold for rounding errors depends on the scaling behaviour of the selected norm and the provenance of \(M\), it is advisable to set tol
explicitly to an appropriate value. Pass tol = 0
to normalize all nonzero vectors.
The generalized Minkowski norm with \(p < 1\) is not homogeneous but can still be normalized. This is numerically unstable for very small values of \(p\), which will be rejected with an error message. The Hamming length (\(p = 0\)) cannot be normalized at all. See rowNorms
for more information.
See rowNorms
for details on available norms and their parameters.