Get the diagonal of a matrix

In matlab, we have

d = diag(D)

It seems that there is no such function in 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.
using LinearAlgebra
diag(rand(3,3))
3 Likes

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

3 Likes

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.

6 Likes

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

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.

3 Likes

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

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> 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).

2 Likes

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


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