A is a vector, then
returns the norm of the vector.
A is a matrix, then
returns the norm of each column.
A is a multidimensional array, then
vecnorm returns the norm along the first array
dimension whose size does not equal 1.
Calculate the 2-norm of a vector corresponding to the point (2,2,2) in 3-D space. The 2-norm is equal to the Euclidean length of the vector, .
x = [2 2 2]; n = vecnorm(x)
n = 3.4641
Calculate the 1-norm of the vector, which is the sum of the element magnitudes.
n = vecnorm(x,1)
n = 6
Calculate the 2-norm of the columns of a matrix.
A = [2 0 1;-1 1 0;-3 3 0]
A = 3×3 2 0 1 -1 1 0 -3 3 0
n = vecnorm(A)
n = 1×3 3.7417 3.1623 1.0000
As an alternative, you can use the
norm function to calculate the 2-norm of the entire matrix.
A— Input array
Input array, specified as a vector, matrix, or multidimensional array. By
values if the vector being operated on contains a
Complex Number Support: Yes
p— Norm type
Norm type, specified as
2 (default), a positive scalar,
dim— Dimension to operate along
Dimension to operate along, specified as a positive integer scalar. If you do not specify a value, then the default is the first array dimension whose size does not equal 1.
dim indicates the dimension whose length
reduces to 1. In other words,
1, while the sizes of all other dimensions remain the
Consider a two-dimensional input array,
vecnorm(A,p,1) calculates the norm of each
vecnorm(A,p,2) calculates the norm of each
dim is greater than
ndims(A) or when
The Euclidean norm (also called the vector magnitude, Euclidean
length, or 2-norm) of a vector
elements is defined by
The general definition for the p-norm of a vector
v that has
N elements is
p is any positive real value or
Some interesting values of
p = 1, then the resulting 1-norm is the sum of the
absolute values of the vector elements.
p = 2, then the resulting 2-norm gives the vector
magnitude or Euclidean length of the vector.
p = Inf, then .
This function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
If you supply
dim, then it must be constant.
For limitations related to variable-size inputs, see Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Code generation does not support sparse matrix inputs for this function.
backgroundPoolor accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).