vecnorm
Vector-wise norm
Description
N = vecnorm(
returns the 2-norm or
Euclidean
norm of A
)A
:
If
A
is a vector, thenvecnorm
returns the norm of the vector.If
A
is a matrix, thenvecnorm
returns the norm of each column.If
A
is a multidimensional array, thenvecnorm
returns the norm along the first array dimension whose size does not equal 1.
N = vecnorm(
calculates the generalized
vector p-norm.A
,p
)
Examples
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.
Input Arguments
A
— Input array
vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array. By
convention, vecnorm
returns NaN
values if the vector being operated on contains a NaN
value.
Data Types: single
| double
Complex Number Support: Yes
p
— Norm type
2 (default) | positive scalar | Inf
Norm type, specified as 2
(default), a positive scalar,
or Inf
.
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.
Dimension dim
indicates the dimension whose length
reduces to 1. In other words, size(N,dim)
is
1
, while the sizes of all other dimensions remain the
same.
Consider a two-dimensional input array, A
:
vecnorm(A,p,1)
calculates the norm of each column.vecnorm(A,p,2)
calculates the norm of each row.vecnorm
returnsabs(A)
whendim
is greater thanndims(A)
or whensize(A,dim)
is1
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
More About
Euclidean Norm
The Euclidean norm (also called the vector magnitude, Euclidean
length, or 2-norm) of a vector v
with N
elements is defined by
General Vector Norm
The general definition for the p-norm of a vector
v
that has N
elements is
where p
is any positive real value or Inf
.
Some interesting values of p
are:
If
p = 1
, then the resulting 1-norm is the sum of the absolute values of the vector elements.If
p = 2
, then the resulting 2-norm gives the vector magnitude or Euclidean length of the vector.If
p = Inf
, then .
Extended Capabilities
Tall Arrays
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.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
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).
Distributed Arrays
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).
Version History
Introduced in R2017b
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)