vecnorm - Vector-wise norm - MATLAB (original) (raw)

Syntax

Description

N = vecnorm([A](#d126e1977672)) returns the 2-norm orEuclidean norm of A:

example

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.

example

Examples

collapse all

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

collapse all

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:

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

More About

collapse all

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:

Extended Capabilities

expand all

Thevecnorm function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

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