Eliminating Diagonal in LowerTriangular

Does anyone know if it is possible to not include the diagonal when using the LowerTriangular() function or if there is a function that can exclude the diagonal from the output? LowerTriangular() would work perfectly for what I am doing if the diagonal was not included.

No, but there is LinAlg.UnitLowerTriangular which masks the diagonal with 1.

Or you can define your own type?

2 Likes

You could use a view of a triangular matrix

view(L,:,2:end)

No, unfortunately @view L[:, 2:end] will just drop the first column. The closest thing here is probably the tril and tril! functions, which allow a second argument to specify the diagonal from which the given matrix should be masked.

Also, is this related? Perhaps the same question?

3 Likes

Just a concrete example to emphasize @mbauman’s idea:

julia> using LinearAlgebra

julia> a = rand(1:5,5,5)
5×5 Array{Int64,2}:
 2  5  2  4  1
 2  1  3  2  3
 1  4  4  3  1
 3  3  1  2  2
 3  2  3  5  3

julia> b = tril(a,-1)
5×5 Array{Int64,2}:
 0  0  0  0  0
 2  0  0  0  0
 1  4  0  0  0
 3  3  1  0  0
 3  2  3  5  0

So, as you see, you just need to feed that optional -1 to the tril function to express your desire for dropping the main diagonal.

2 Likes

Thank you for the example!