Written and maintained by CASRAI Editorial Board
Published
On this page
To get the norm of a vector in MATLAB, call norm(v): it returns the Euclidean length (the 2-norm). Pass a second argument for another p-norm, as in norm(v,1) or norm(v,Inf). To get one norm per column or row of a matrix, use vecnorm instead, because norm applied to a matrix returns a matrix norm, not a set of vector norms. This guide covers the syntax of both functions, a worked hand-check you can verify against your own output, the unit-vector idiom, and the mistakes that most often produce a surprising number.
What the norm of a vector means in research code
A vector norm is a single non-negative number that measures the size of a vector. In analysis scripts it appears as the length of a displacement, the magnitude of a residual vector, the distance between two observations, or the quantity you divide by to scale a vector to unit length. MATLAB defines the p-norm of a vector as the following sum (MathWorks norm reference, read 2026-11-08):
||v||p = ( sum over k of |vk|^p )^(1/p)
The special cases that matter in practice are p = 1 (the sum of absolute values), p = 2 (the Euclidean length) and p = Inf (the largest absolute element).
Install or access
norm and vecnorm are documented in the core MATLAB function reference at mathworks.com/help/matlab/ref/norm.html and mathworks.com/help/matlab/ref/vecnorm.html. vecnorm was introduced in release R2017b, so scripts that use it will not run on older releases. Check your release with version in the Command Window, and obtain MATLAB only from MathWorks or through your institution’s licence.
How to compute a vector norm in MATLAB (step by step)
Step 1: Create the vector
Row and column vectors both work. This example is the one used in the MathWorks documentation:
v = [1 -2 3];
Step 2: Norm of a vector, the 2-norm
The answer to “norm of a vector in MATLAB” with no further arguments is the Euclidean norm:
n = norm(v)
n = 3.7417
Check by hand: 12 + (-2)2 + 32 = 14, and the square root of 14 is 3.7417.
Step 3: Norm of a vector with norm(v,p)
The second argument p selects the norm. The documented allowed values are 2 (the default), any positive real scalar, Inf and -Inf.
norm(v,1) % 6 sum of absolute values: 1 + 2 + 3
norm(v,2) % 3.7417 same as norm(v)
norm(v,Inf) % 3 largest absolute value
norm(v,-Inf) % 1 smallest absolute value
The first, third and fourth results follow directly from the definitions above; the MathWorks page shows the same calls on a different vector, v = [-2 3 -1], where norm(v,1) returns 6.
Step 4: Euclidean distance between two points
The norm of a difference is the distance. MathWorks gives a = [0 3]; b = [-2 1]; d = norm(b-a), which returns d = 2.8284.
Step 5: Norm of each column or row with vecnorm
Use vecnorm(A) for the 2-norm of each column, vecnorm(A,p) for another p, and vecnorm(A,p,dim) to choose the dimension: dim = 1 gives one norm per column, dim = 2 one per row. The documented values of p are 2 (default), any positive scalar, or Inf. With the MathWorks example matrix:
A = [2 0 1; -1 1 0; -3 3 0];
vecnorm(A) % [3.7417 3.1623 1.0000] one value per column
vecnorm(A,2,2) % one value per row (2.2361, 1.4142, 4.2426 by hand: sqrt 5, sqrt 2, sqrt 18)
For a plain vector, vecnorm returns the vector’s norm: vecnorm([2 2 2]) returns 3.4641, the square root of 12.
Choosing between norm, vecnorm and v/norm(v)
| You have | You want | Use | Source |
|---|---|---|---|
| A vector | Its length (2-norm) | norm(v) |
MathWorks norm |
| A vector | 1-norm, Inf-norm or another p-norm | norm(v,p) |
MathWorks norm |
| A matrix | One norm per column | vecnorm(A) or vecnorm(A,p,1) |
MathWorks vecnorm |
| A matrix | One norm per row | vecnorm(A,p,2) |
MathWorks vecnorm |
| A matrix | A single matrix norm (including the 2-norm of the whole matrix) | norm(A), norm(A,p) with p = 1, 2 or Inf, or norm(A,"fro") |
MathWorks norm, vecnorm |
| A vector | A unit vector in the same direction | v/norm(v) |
Editorial arithmetic (divide by the length) |
Output and how to read it
The result is a non-negative scalar for norm(v), and an array whose size along dim has collapsed to 1 for vecnorm; the other dimensions keep their sizes. Compare the output with the hand-check of the same vector:
| Call on v = [1 -2 3] | Hand calculation | Result |
|---|---|---|
norm(v,1) |
1 + 2 + 3 | 6 |
norm(v) |
sqrt(1 + 4 + 9) | 3.7417 |
norm(v,Inf) |
max(1, 2, 3) | 3 |
The same ordering (1-norm at least the 2-norm, at least the Inf-norm) will hold for any vector, so a reversed order in your output points to a typing error in p. This is an editorial observation from the definitions, not a documented MATLAB statement.
Unit vector
Dividing a vector by its 2-norm gives a vector of length 1 in the same direction. For v = [1 -2 3], u = v/norm(v) gives approximately [0.2673 -0.5345 0.8018], and norm(u) returns 1 up to rounding. This is plain arithmetic rather than a documented function behaviour. A zero vector has norm 0, so the division produces NaN values; test for it first if your data can contain all-zero rows. For the Python equivalent, see the CASRAI guide on normalising arrays with NumPy.
Troubleshooting
norm(A) on a matrix does not return one number per column
For matrix input norm returns a matrix norm (norm(X), norm(X,p) with p = 1, 2 or Inf, or norm(X,"fro")). The MathWorks norm page points to vecnorm when you want to treat the array as a collection of vectors. Switch to vecnorm(A,p,dim).
The result is NaN
The vecnorm documentation states that, by convention, the result is NaN when the vector being operated on contains a NaN. Remove or impute missing values first, for example by selecting complete rows before taking the norm.
vecnorm returns abs(A) unchanged
When dim is larger than ndims(A) or size(A,dim) is 1, vecnorm returns abs(A). A common cause is passing dim = 2 for a column vector.
Using a p below 1
MATLAB accepts any positive scalar for p, including values between 0 and 1. Mathematically, the resulting quantity is not a true norm for p below 1 because it violates the triangle inequality (editorial note). Report it as a “p-quasi-norm” or avoid it if a reviewer might expect a norm.
vecnorm is “undefined”
The function was introduced in R2017b; on earlier releases, compute sqrt(sum(abs(A).^2, dim)) for the 2-norm instead (this uses only standard element-wise operations and follows from the definition of the 2-norm).
Free alternatives
| Tool | Same task | Source |
|---|---|---|
| Python with NumPy | numpy.linalg.norm(x, ord=None, axis=None): default is the 2-norm; ord=1 sums absolute values; ord=np.inf gives the maximum absolute value; an integer axis gives one vector norm per slice, like vecnorm. For v = [1, -2, 3] it returns 3.7416573867739413. |
NumPy documentation for numpy.linalg.norm, read 2026-11-08 |
The CASRAI guide to numpy.linalg.norm covers this function in detail. Related MATLAB tasks on this site include the inverse of a matrix in MATLAB and computing eigenvalues in MATLAB.
Frequently asked questions
What is the norm of a vector in MATLAB?
By default, norm(v) returns the Euclidean norm, the square root of the sum of squared absolute values. For v = [1 -2 3] that is 3.7417.
How do I write the norm of a vector in MATLAB for the 1-norm or infinity norm?
Use norm(v,1) for the sum of absolute values and norm(v,Inf) for the largest absolute value. norm(v,-Inf) returns the smallest.
How do I get the norm of every row or column?
Use vecnorm(A,2,1) for columns and vecnorm(A,2,2) for rows (the 2 in the second position is the default p).
How do I normalise a vector to length 1?
Compute v/norm(v). The check norm(v/norm(v)) should return 1 to within rounding.
Sources
- MathWorks. norm: Vector and matrix norms. MATLAB documentation, read 2026-11-08.
- MathWorks. vecnorm: Vector-wise norm. MATLAB documentation, read 2026-11-08.
- NumPy. numpy.linalg.norm. NumPy reference, read 2026-11-08.







