# Get the diagonal of a matrix

**URL:** https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286
**Category:** General Usage
**Tags:** question
**Created:** [January 3, 2024, 8:47am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286 "2024-01-03T08:47:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [January 3, 2024, 8:47am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/1 "2024-01-03T08:47:17Z")

</div>

In matlab, we have

```julia
d = diag(D)

```

It seems that there is no such function in julia?

```julia
help?> diag
search: spdiagm blockdiag OrdinalRange disable_sigint ENDIAN_BOM dirname isdirpath isdispatchtuple display displaysize displayable redisplay popdisplay diskstat pushdisplay TextDisplay PermutedDimsArray

Couldn't find diag
Perhaps you meant dim, diff, div, big, imag, spdiagm, Dict, Dims, edit, fdio, ndims, dump, do, ind, bind, lpad, read, rpad, sind, a, eval, Cint, Libc, NaN, Pipe, Val, atan, cat, cis, fill, fma, hcat, im, in or inv No documentation found.

  Binding diag does not exist.

```

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [January 3, 2024, 8:50am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/2 "2024-01-03T08:50:50Z")

</div>

```julia
using LinearAlgebra
diag(rand(3,3))

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 3, 2024, 8:58am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/3 "2024-01-03T08:58:17Z")

</div>

You might want to use the search functionality of the Julia documentation and JuliaHub for questions like this.

[https://docs.julialang.org/en/v1/search/?q=diag](https://docs.julialang.org/en/v1/search/?q=diag)

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [January 3, 2024, 9:01am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/4 "2024-01-03T09:01:49Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> ```julia
> help?> diag
> search: spdiagm blockdiag OrdinalRange disable_sigint ENDIAN_BOM dirname isdirpath isdispatchtuple display displaysize displayable redisplay popdisplay diskstat pushdisplay TextDisplay PermutedDimsArray
> 
> Couldn't find diag
> 
> ```

While both base Julia and MATLAB are bundled with a lot of numerical computing code, not all names are immediately available and import statements are used to load code and make names available. In the example above, `using LinearAlgebra` loaded the package and made its exported names available, including `diag`.

Since `help?>` only sees the available names, you should use alternative ways to find names that haven’t been imported yet. Searching the terms in a browser often works because they managed to pick up a lot of webpages. If you think a name or feature might be available in a documentation, you can look in those specifically. As mentioned, some documentation websites have a search bar you can use to find possible page matches, narrowing down what you have to look through. Even an imprecise search “diagonal” will end up lising `LinearAlgebra.diag`.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 3, 2024, 9:37am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/5 "2024-01-03T09:37:34Z")

</div>

If you want to stick to the REPL you can use the somewhat more noisy `apropos`:

```julia
julia> apropos("diag")
Base.cat
Base.print_matrix
Base.permutedims
Base.Meta.parse
Base.JuliaSyntax.parse!
Base.JuliaSyntax.emit_diagnostic
Base.JuliaSyntax.Diagnostic
LinearAlgebra.istriu
LinearAlgebra.QRCompactWY
(...)
LinearAlgebra.diag

```

which also works by feeding a string into the help mode (`help?> "diag"`). As you can see above it can be noisy, but on the upside it can find things where the name of the function is maybe unexpected to you but the docstring contains what you’re after. It also takes regexes so a well crafted query can reduce the noise.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [January 3, 2024, 11:28am UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/6 "2024-01-03T11:28:16Z")

</div>

Perhaps you are looking for `diagind` combined with `@view` as follows:

```julia
julia> M = rand(3,3)
3×3 Matrix{Float64}:
 0.00896915 0.857195 0.693312
 0.860858 0.852806 0.0328728
 0.317448 0.0254451 0.50126

julia> d = @view(M[diagind(M)])
3-element view(::Vector{Float64}, 1:4:9) with eltype Float64:
 0.00896915244921559
 0.8528062299441197
 0.5012600761484215

```

Note that:

```julia
julia> d[1] = 10.0
10.0

julia> M
3×3 Matrix{Float64}:
 10.0 0.857195 0.693312
  0.860858 0.852806 0.0328728
  0.317448 0.0254451 0.50126

```

This has the advantage of not allocating (usually).

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [January 3, 2024, 12:09pm UTC](https://discourse.julialang.org/t/get-the-diagonal-of-a-matrix/108286/7 "2024-01-03T12:09:06Z")

</div>

could you define your own diag() function (using diagind suggestion)

```julia

diag(m)=@view m[1:size(m)[rand(1:2)]+1:prod(size(m))]
diag(m)

```
