Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector norm of an array of vectors in MATLAB

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?

like image 823
Amelio Vazquez-Reina Avatar asked Aug 26 '11 19:08

Amelio Vazquez-Reina


People also ask

How do you find the norm of a vector in MATLAB?

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)) .

What is norm () in MATLAB?

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.

How does MATLAB calculate L2 norm?

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.

How do you find the norm of a vector?

The norm of a vector is simply the square root of the sum of each component squared.


2 Answers

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.

like image 165
gnovice Avatar answered Sep 30 '22 00:09

gnovice


From version 2017b onwards, you can use vecnorm.

like image 40
Xaser Avatar answered Sep 30 '22 01:09

Xaser