Last chance! 50% off unlimited learning
Sale ends in
Last chance! 50% off unlimited learning
Sale ends in
Efficiently compute the norms of all row or column vectors of a dense or sparse matrix.
rowNorms(M, method = "euclidean", p = 2)colNorms(M, method = "euclidean", p = 2)
a dense or sparse numeric matrix
norm to be computed (see “Norms” below for details)
exponent of the minkowski
p-norm, a numeric value in the range \(1 \le p \le \infty\).
Values \(0 \le p < 1\) are also permitted as an extension but do not correspond to a proper mathematical norm (see details below).
A numeric vector containing one norm value for each row or column of M
.
Given a row or column vector \(x\), the following length measures can be computed:
euclidean
The Euclidean norm given by $$ \|x\|_2 = \sqrt{ \sum_i x_i^2 }$$
maximum
The maximum norm given by $$ \|x\|_{\infty} = \max_i |x_i| $$
manhattan
The Manhattan norm given by $$ \|x\|_1 = \sum_i |x_i| $$
minkowski
The Minkowski (or \(L_p\)) norm given by $$ \|x\|_p = \left( \sum_i |x_i|^p \right)^{1/p} $$ for \(p \ge 1\). The Euclidean (\(p = 2\)) and Manhattan (\(p = 1\)) norms are special cases, and the maximum norm corresponds to the limit for \(p \to \infty\).
As an extension, values \(0\le p < 1\) compute the length measure $$ \|x\|_p = \sum_i |x_i|^p $$ This formula does not define a proper mathematical norm because it is not homogeneous (\(\|r\cdot x\| \ne |r|\cdot \|x\|\) for a scalar factor \(r\)). However, it does satisfy the triangle inequality and is thus still a sensible measure of vector length. In the special case \(p = 0\), the length \(\|x\|_0\) corresponds to the number of nonzero elements in the vector \(x\).
# NOT RUN {
rowNorms(DSM_TermContextMatrix, "manhattan")
colNorms(DSM_TermContextMatrix, "minkowski", p=0) # number of nonzero cells
# }
Run the code above in your browser using DataLab