vecnorm(A, [p])

For any iterable container A (including arrays of any dimension) of numbers (or any element type for which norm is defined), compute the p-norm (defaulting to p=2) as if A were a vector of the corresponding length.

For example, if A is a matrix and p=2, then this is equivalent to the Frobenius norm.


  1. Compute the Euclidean norm of a vector:

    julia> vecnorm([3, 4])

    This example calculates the Euclidean norm (p=2 norm) of the vector [3, 4].

  2. Calculate the 1-norm of a matrix:

    julia> mat = [1 2; -3 4];
    julia> vecnorm(mat, 1)

    It computes the 1-norm (sum of absolute values) of the matrix mat treating it as a vector.

  3. Calculate the infinity norm of a matrix:

    julia> mat = [1 2; -3 4];
    julia> vecnorm(mat, Inf)

    This example computes the infinity norm (maximum absolute row sum) of the matrix mat as if it were a vector.

  4. Compute the Frobenius norm of a matrix:
    julia> mat = [1 2; -3 4];
    julia> vecnorm(mat)

    It calculates the Frobenius norm (p=2 norm) of the matrix mat.

Common mistake example:

julia> vecnorm([1, 2, 3], -1)
ERROR: ArgumentError: Invalid value for p-norm: -1

In this example, -1 is provided as the p value, which is not a valid input. The p value should be a positive number or Inf.

See Also

countfrom, cycle, done, drop, eachindex, enumerate, first, repeated, rest, start, svds, take, vecdot, vecnorm, zip,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: