vecnorm

Vector norm of each column or row of a matrix.

Category: Mathematics > Linear Algebra

Syntax

N = vecnorm(A)

Returns the 2-norm of each column of A.

N = vecnorm(A, p)

Uses the p-norm.

N = vecnorm(A, p, dim)

Takes the norm along dimension dim; dim = 2 works on rows.

Description

Unlike norm, which treats a matrix as an operator, vecnorm treats every column (or row) as a separate vector and returns all the lengths at once. It replaces the common idiom sqrt(sum(abs(A).^2)).

With p = Inf it returns the largest absolute value, and with p = 1 the sum of absolute values.

Input arguments

A
Numeric array.
p
Positive real scalar or Inf. Default 2.
dim
Dimension along which to operate. Default: the first non-singleton dimension.

Output arguments

N
Array of norms; the operated dimension has size 1.

Example

Columns and rows

A = [3 4; 6 8];
vecnorm(A)
vecnorm(A, 1, 2)
vecnorm(A, Inf, 2)
Output
ans = 1×2

    6.7082    8.9443
ans = 2×1

     7
    14
ans = 2×1

     4
     8

See also

norm normalize dot cross sum