# Eliminating Diagonal in LowerTriangular

**URL:** <https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012>\
**Category:** Numerics\
**Tags:** linearalgebra\
**Created:** [June 27, 2018, 8:08pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012 "2018-06-27T20:08:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![avoss3](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@avoss3](https://discourse.julialang.org/u/avoss3)\
**Post date:** [June 27, 2018, 8:08pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/1 "2018-06-27T20:08:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [June 27, 2018, 8:13pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/2 "2018-06-27T20:13:04Z")

</div>

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

Or you can define your own type?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 27, 2018, 8:15pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/3 "2018-06-27T20:15:42Z")

</div>

You could use a view of a triangular matrix

```julia
view(L,:,2:end)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 27, 2018, 10:03pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/4 "2018-06-27T22:03:27Z")

</div>

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?

> <https://stackoverflow.com/questions/51068263/lower-triangular-matrix-equal-to-value-in-julia>

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 27, 2018, 10:42pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/5 "2018-06-27T22:42:28Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![avoss3](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@avoss3](https://discourse.julialang.org/u/avoss3)\
**Post date:** [June 28, 2018, 1:13pm UTC](https://discourse.julialang.org/t/eliminating-diagonal-in-lowertriangular/12012/6 "2018-06-28T13:13:11Z")

</div>

Thank you for the example!
