# Eigenvalue calculation differs between Julia and MATLAB

**URL:** <https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185>\
**Category:** Numerics\
**Created:** [November 17, 2019, 5:22pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185 "2019-11-17T17:22:34Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![gh\_ali83](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@gh\_ali83](https://discourse.julialang.org/u/gh_ali83)\
**Post date:** [January 8, 2021, 4:41pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/22 "2021-01-08T16:41:37Z")

</div>

thanks a lot. do you know how I can have same response in matlab and python?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [January 8, 2021, 4:51pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/23 "2021-01-08T16:51:15Z")

</div>

I think the question is: why do you want to do this at all? …given that you understood what eigenvalues and vectors are.

Again, everything is in this thread 😉 see the answer marked as solution: [Eigenvalue calculation differs between Julia and MATLAB - #18 by GunnarFarneback](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/18)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 8, 2021, 5:02pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/24 "2021-01-08T17:02:52Z")

</div>

So… with matrix A\in \mathbb{C}^{n\times n} and eigenvalues \lambda \in \mathbb{C}^n, for the particular eigenvalue \lambda\_j \in \mathbb{C}, an eigenvector v\_j \in \mathbb{C}^n satisfies:

A\lambda\_j = \lambda\_j v\_j \Leftrightarrow (A-\lambda\_j I)v\_j =0

Here, eigenvalue \lambda\_j _by design_ ensures that A-\lambda\_j I has rank loss: \det (A-\lambda\_j I) \equiv 0. It follows that the eigenvector v\_j lies in the non-trivial nullspace of A-\lambda\_j I,

v\_j \in {\cal N} (A-\lambda\_j I)

Because of the properties of the nullspace, if v\_j is an eigenvector, then so is any other vector u that satisfies u \parallel v\_j, in other words: u=c\cdot v\_j where c is a complex number is also an eigenvector for eigenvalue \lambda\_j.

It is common to scale v\_j so that the length is unity, although this is not required for v\_j to be an eigenvector. And even with unity length, the sign can differ.

In addition to this, the eigenvalues — and hence the corresponding eigenvectors can appear in any order. In principle. Unless a particular ordering of the eigenvalues is enforced.

So — MATLAB and Python (and Julia) may have different rules for ordering the eigenvalues \lambda\_j, and they may also have different rules for choosing the sign of the eigenvectors v\_j.

As `tamasgal` indicates — is it important that the eigenvalues and eigenvectors appear in identical order and with identical value/sign?

---

<div class="post-metadata">

**Author:** ![gh\_ali83](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@gh\_ali83](https://discourse.julialang.org/u/gh_ali83)\
**Post date:** [January 8, 2021, 5:05pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/25 "2021-01-08T17:05:00Z")

</div>

yes I understood. but I am coding a matlab code to python and in this stage result for matlab and python is different. I don’t know what I have to for same result?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [January 8, 2021, 5:10pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/26 "2021-01-08T17:10:38Z")

</div>

I think you would want to sort all the eigensolutions in the manner you prefer, e.g. absolute value, and normalize the eigenvectors in the same way, both in matlab and python. This should, in principle, give you comparable results.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 8, 2021, 5:16pm UTC](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185/27 "2021-01-08T17:16:24Z")

</div>

So you need to figure out how MATLAB and Python and/or Julia sorts the eigenvalues. For your system:

```julia
julia> a = [1 2 1.5 2;2 2 0 0;1.5 0 1 0;2 0 0 3]
julia> lam = eigvals(a) |> show
[-1.7369215679264496, 1.276789240956863, 2.5000000000000004, 4.9601323269
69587]
julia> v = eigvecs(a)
4×4 Array{Float64,2}:
 -0.752694 -0.159387 -0.171499 -0.615334
  0.402842 0.440777 -0.685994 -0.415748
  0.412522 -0.863765 -0.171499 -0.233073
  0.317799 0.184989 0.685994 -0.627849

```

In this case, the eigenvalues are all real, and it seems like Julia sorts the eigenvalues in order of increasing value.

If you want to sort the eigenvalues in order of increasing _absolute value_ of the eigenvalues, you would get eigenvalues:

\lambda = (1.276789..., -1.736921..., 2.5, 4.960...)

Then you need to permute the columns of matrix `v` correspondingly.  
If you want to have a particular sign for the columns (eigenvectors), you can do that, too. Some possibilities:

- the first element of the eigenvectors should have positive value
- the majority of elements in the eigenvectors should have positive value
- etc.

In summary: to get correspondence between MATLAB and Julia (since this is a Julia forum), you need to figure out (i) MATLAB’s system of ordering the eigenvalues, and (ii) MATLAB’s system of choosing sign of eigenvectors.

[Previous page](https://discourse.julialang.org/t/eigenvalue-calculation-differs-between-julia-and-matlab/31185.md?page=1)
