HOWTO · MATLAB

MATLAB Norm() Function

Learn how MATLAB norm() computes vector and matrix norms, how to choose a norm, and how vecnorm() handles rows or columns.

On this page

MATLAB norm() computes the size of a vector or matrix. Its default depends on the input: a vector uses the 2-norm, while a matrix uses the matrix 2-norm, also called the spectral norm. Choose an explicit order or option when you need a different measure. For norms of individual rows or columns, use vecnorm().

MATLAB norm() Syntax and Default Behavior

Use n = norm(X) for the default, n = norm(X,p) to select an order, or n = norm(X,"fro") to request the Frobenius norm of a matrix. X is the input vector or matrix. The meaning of p depends on whether X is a vector or a matrix, so the same order does not always describe the same calculation.

For a vector, norm(v) is equivalent to norm(v,2). For a matrix, norm(A) is equivalent to norm(A,2): it returns the largest singular value, not the Frobenius norm. The Frobenius calculation is selected explicitly with "fro".

Choose a Vector Norm

For a vector v, the p-norm for a positive finite p is the pth root of the sum of the absolute values of its elements raised to p. Common choices are norm(v,1), the sum of absolute values; norm(v,2), the Euclidean length; and norm(v,Inf), the largest absolute element. Omitting p selects the 2-norm.

Use the 1-norm when total absolute magnitude is useful, the 2-norm for Euclidean length, or the infinity-norm when the largest component determines the scale. These are different summaries of the same vector, so choose based on what your calculation needs. For example, a vector with components 3 and 4 has 1-norm 7, 2-norm 5, and infinity-norm 4.

Choose a Matrix Norm

For a matrix A, MATLAB supports the 1-, 2-, and infinity-norms through norm(A,p), with definitions that differ from the vector case. The matrix 1-norm is the maximum absolute column sum. The matrix 2-norm is the largest singular value and is the default. The matrix infinity-norm is the maximum absolute row sum. Use norm(A,"fro") for the Frobenius norm, the square root of the sum of the squared absolute values of all entries.

For instance, for A = [1 2; 3 4], the 1-norm is 6 and the infinity-norm is 7. The Frobenius norm is the square root of 30, while the 2-norm is the largest singular value (approximately 5.465). Do not infer a matrix’s Frobenius norm from norm(A); request it with "fro".

Run an Example and Read the Results

Run this example:

v = [3 4];
A = [1 2; 3 4];
fprintf('vector 2-norm: %.1f\n', norm(v));
fprintf('matrix default (2-norm): %.4f\n', norm(A));
fprintf('matrix 1-norm: %.1f\n', norm(A, 1));
fprintf('matrix infinity-norm: %.1f\n', norm(A, Inf));
fprintf('matrix Frobenius norm: %.4f\n', norm(A, "fro"));
vector 2-norm: 5.0
matrix default (2-norm): 5.4650
matrix 1-norm: 6.0
matrix infinity-norm: 7.0
matrix Frobenius norm: 5.4772

The vector result is its Euclidean length. For the matrix, the default and explicit order 2 both return the spectral norm; the other lines show the maximum column sum, maximum row sum, and explicitly requested Frobenius norm. The values are useful for confirming which definition a calculation uses.

Compute Norms Along Rows or Columns with vecnorm()

norm() returns one norm for the entire input. To get a separate norm for each column or row, call vecnorm(X,p,dim). Its dim argument identifies the dimension along which to calculate. For a two-dimensional matrix, dimension 1 operates down each column and returns one value per column; dimension 2 operates across each row and returns one value per row. If omitted, vecnorm() uses the first nonsingleton dimension, which for an ordinary matrix means columns.

For example, use vecnorm(A,2,1) for the 2-norm of each column and vecnorm(A,2,2) for the 2-norm of each row. This is the per-dimension operation; adding a dimension argument to norm() is not the way to request row-wise or column-wise norms. Choose norm() for one measure of the full vector or matrix, and vecnorm() when the result should contain a norm for each slice along a dimension.