# JULIA matrix norm is different MATLAB

**URL:** <https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 3, 2021, 2:38am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500 "2021-12-03T02:38:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [December 3, 2021, 2:38am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/1 "2021-12-03T02:38:46Z")

</div>

Hi,  
when I calculate the norm of vector a3, I get different values ​​in julia vs MatLab

![julia](https://global.discourse-cdn.com/julialang/original/3X/7/c/7cc9f087171b15f0183437836dbdc146570a0f21.png)

![matlab](https://global.discourse-cdn.com/julialang/original/3X/7/5/752a0a2e80e656b0973fa8a80ab8d3d72d8bc17a.png)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 3, 2021, 2:50am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/2 "2021-12-03T02:50:47Z")

</div>

If I copied your matrix correctly:

```julia
julia> a3 = [0 sqrt(2)/2 sqrt(2) sqrt(2)/2 0 -sqrt(2)/2 -sqrt(2) -sqrt(2)/2;
             -sqrt(2)/2 -sqrt(2) 0 sqrt(2)/2 sqrt(2) -sqrt(2)/2 0 -sqrt(2)/2]
2×8 Matrix{Float64}:
  0.0 0.707107 1.41421 0.707107 0.0 -0.707107 -1.41421 -0.707107
 -0.707107 -1.41421 0.0 0.707107 1.41421 -0.707107 0.0 -0.707107

julia> norm(a3)
3.464101615137755

```

the result looks correct to me: the sum of the squares of the elements is 12, it’s square root is 3.464…, which is the 2-norm. Does Matlab give the 2-norm as well with the `norm` function?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 3, 2021, 2:51am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/3 "2021-12-03T02:51:23Z")

</div>

> **[Vector and matrix norms - MATLAB norm](https://www.mathworks.com/help/matlab/ref/norm.html#bvhji30-1)**
>
> This MATLAB function returns the Euclidean norm of vector v.

wait , no

> `n` = norm([`X`](https://www.mathworks.com/help/matlab/ref/norm.html#bt0y64b-1-X)) returns the 2-norm or maximum singular value of matrix `X` , which is approximately `max(svd(X))` .

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 3, 2021, 2:54am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/4 "2021-12-03T02:54:08Z")

</div>

Matlab’s `norm` gives you the operator norm for matrices, whereas Julia’s `norm` will always give you the standard L2 norm. The equivalent in Julia would be `opnorm`:

```julia
julia> using LinearAlgebra

julia> a3 = [0 sqrt(2)/2 sqrt(2) sqrt(2)/2 0 -sqrt(2)/2 -sqrt(2) -sqrt(2)/2;
             -sqrt(2)/2 -sqrt(2) 0 sqrt(2)/2 sqrt(2) -sqrt(2)/2 0 -sqrt(2)/2]
2×8 Matrix{Float64}:
  0.0 0.707107 … -1.41421 -0.707107
 -0.707107 -1.41421 0.0 -0.707107

julia> opnorm(a3)
2.5495097567963927

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 3, 2021, 2:58am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/5 "2021-12-03T02:58:11Z")

</div>

Perhaps this might be worth mentioning in [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB), since it is a bit of a gotcha, if anyone cares to make a PR.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 3, 2021, 3:20am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/6 "2021-12-03T03:20:41Z")

</div>

Actually, the previous posts have it backwards, as far as the names of the norms are concerned. For matrices, it’s `opnorm` that gives you the induced matrix 2-norm, \|A\|\_2 = \sup\_{x\neq0} \|Ax\|\_2/\|x\|\_2 = max singular value, whereas `norm` gives the root-sum-squares Frobenius norm, \|A\|\_F = \sqrt{\sum\_{i,j} A^2\_{ij}}.

E.g.

```julia
julia> A = [1 1; 2 0]
2×2 Matrix{Int64}:
 1 1
 2 0

julia> opnorm(A)
2.2882456112707374

julia> svdvals(A)
2-element Vector{Float64}:
 2.2882456112707374
 0.8740320488976421

julia> norm(A)
2.449489742783178

julia> sqrt(1^2 + 1^2 + 2^2 + 0^2)
2.449489742783178

```

That’s what documentation says as well

```julia
help?> opnorm
search: opnorm

  opnorm(A::AbstractMatrix, p::Real=2)

  Compute the operator norm (or matrix norm) induced by the vector p-norm, where valid values of p are 1,
  2, or Inf. (Note that for sparse matrices, p=2 is currently not implemented.) Use norm to compute the
  Frobenius norm.

```

and

```julia
help?> norm
search: norm normpath normalize normalize! opnorm issubnormal UniformScaling ColumnNorm set_zero_subnormals

  norm(A, p::Real=2)

  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.

```

Note the “as if A were a vector of the corresponding length.” I.e. unpack A into a vector and compute the 2-norm of that vector. That is totally different from the induced matrix 2-norm.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 3, 2021, 3:24am UTC](https://discourse.julialang.org/t/julia-matrix-norm-is-different-matlab/72500/7 "2021-12-03T03:24:42Z")

</div>

Matlab’s `norm` applied to a matrix gives the induced matrix 2-norm, equal to the matrices’ largest singular value.

Julia’s `norm` applied to a matrix gives the Frobenius norm, equal to the root sum of squares of the matrix elements.

Presumably Julia uses the Frobenius norm because it’s way cheaper to compute root sum of squares than an SVD.

And also, the title of the OP is wrong. This is a different of matrix norms, not vector norms.
