Ais a vector, then
vecnormreturns the norm of the vector.
Ais a matrix, then
vecnormreturns the norm of each column.
Ais a multidimensional array, then
vecnormreturns the norm along the first array dimension whose size does not equal 1.
1-Norm and 2-Norm of Vector
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
2-Norm of Matrix Columns
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
vector | matrix | multidimensional 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
2 (default) | positive scalar |
Norm type, specified as
2 (default), a positive scalar,
dim — Dimension to operate along
positive integer scalar
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 column.
vecnorm(A,p,2)calculates the norm of each row.
dimis greater than
The Euclidean norm (also called the vector magnitude, Euclidean
length, or 2-norm) of a vector
elements is defined by
General Vector Norm
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 .
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
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.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).