When calling norm
on a matrix in MATLAB, it returns what's known as a "matrix norm" (a scalar value), instead of an array of vector norms. Is there any way to obtain the norm of each vector in a matrix without looping and taking advantage of MATLAB's vectorization?
n = norm( v ) returns the Euclidean norm of vector v . This norm is also called the 2-norm, vector magnitude, or Euclidean length. n = norm( v , p ) returns the generalized vector p-norm. n = norm( X ) returns the 2-norm or maximum singular value of matrix X , which is approximately max(svd(X)) .
n = norm(X) returns the 2-norm of input X and is equivalent to norm(X,2). If X is a vector, this is equal to the Euclidean distance. If X is a matrix, this is equal to the largest singular value of X. The 2-norm is equal to the Euclidean length of the vector.
Direct link to this answer The L2-norm of a matrix, |A|||_2, ( norm(A, 2) in MATLAB) is an operator norm, which is computed as max(svd(A)). For a vector x, the norm |x|||_2, ( norm(x, 2) in MATLAB), is a vector norm, defined as sqrt(sum(x.
The norm of a vector is simply the square root of the sum of each component squared.
You can compute the norm of each column or row of a matrix yourself by using element-wise arithmetic operators and functions defined to operate over given matrix dimensions (like SUM and MAX). Here's how you could compute some column-wise norms for a matrix M
:
twoNorm = sqrt(sum(abs(M).^2,1)); %# The two-norm of each column
pNorm = sum(abs(M).^p,1).^(1/p); %# The p-norm of each column (define p first)
infNorm = max(M,[],1); %# The infinity norm (max value) of each column
These norms can easily be made to operate on the rows instead of the columns by changing the dimension arguments from ...,1
to ...,2
.
From version 2017b onwards, you can use vecnorm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With