vecnorm - Vector-wise norm - MATLAB (original) (raw)
Syntax
Description
N = vecnorm([A](#d126e1977672))
returns the 2-norm orEuclidean norm of 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([A](#d126e1977672),[p](#d126e1977713),[dim](#d126e1977741))
operates along dimension dim
. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.
Examples
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, 12.
x = [2 2 2]; n = vecnorm(x)
Calculate the 1-norm of the vector, which is the sum of the element magnitudes.
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 = 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
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
Norm type, specified as 2
(default), a positive scalar, or Inf
.
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)
is1
, 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
The Euclidean norm (also called the vector magnitude, Euclidean length, or 2-norm) of a vector v
with N
elements is defined by
The general definition for the p-norm of a vectorv
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 ‖v‖∞=maxi(|v(i)|).
Extended Capabilities
Thevecnorm
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- Code generation does not support sparse matrix inputs for this function.
The vecnorm
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2017b