# How to set all elements in a lower triangular matrix?

**URL:** https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603
**Category:** General Usage
**Tags:** linearalgebra, matrices
**Created:** [May 1, 2025, 9:21pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603 "2025-05-01T21:21:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Abhro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhro/32/220753_2.png) [@Abhro](https://discourse.julialang.org/u/Abhro)
#### Post date: [May 1, 2025, 9:21pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/1 "2025-05-01T21:21:09Z")

</div>

I have a lower triangular matrix which gets reused. In one of the steps, I have all of the elements (below the diagonal) set to the same value. How do I make Julia understand that I’m not trying to set values above the diagonal?

MWE:

```julia-repl
julia> using LinearAlgebra

julia> JB = LowerTriangular(zeros(Float64, 3, 3))
3×3 LowerTriangular{Float64, Matrix{Float64}}:
 0.0 ⋅ ⋅
 0.0 0.0 ⋅
 0.0 0.0 0.0

julia> JB .= -5
ERROR: ArgumentError: cannot set indices in the upper triangular part of an LowerTriangular matrix to a nonzero value (-5)
[...]

```

What I’m looking for is something like

```julia-repl
julia> JB .= -5
3×3 LowerTriangular{Float64, Matrix{Float64}}:
 -5.0 ⋅ ⋅
 -5.0 -5.0 ⋅
 -5.0 -5.0 -5.0

```

where the matrix structure is still preserved, and only the non-zero elements are set.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 1, 2025, 9:51pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/2 "2025-05-01T21:51:42Z")

</div>

```julia
function setall!(A::LowerTriangular, value)
  for col=1:size(A,2), row=col:size(A,1) A[row,col] = value end
  A
end

setall!(JB, -5.0)

```

or, if you must use `.=`

```julia
ltindices = [c for c in CartesianIndices(JB) if c[1]>=c[2]]
JB[ltindices] .= -5.0

```

---

<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: [May 1, 2025, 10:35pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/3 "2025-05-01T22:35:21Z")

</div>

Another way can be:

```julia
function setall2!(A::LowerTriangular, value)
    A.data .= value
    A
end

```

This works faster than `setall!` (see previous post), but at the cost of going into the internal details of `LowerTriangular`. Note that `LowerTriangular` doesn’t store the matrix in a compressed form, but instead stores (and ignores) the other half of the matrix.  
Doing the index calculations is slower than setting the invisible part of the matrix.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 2, 2025, 5:36am UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/4 "2025-05-02T05:36:14Z")

</div>

Instead of using internals you can just call `parent`:

```julia
julia> T = LowerTriangular(rand(3, 3))
3×3 LowerTriangular{Float64, Matrix{Float64}}:
 0.171761 ⋅ ⋅ 
 0.381324 0.648652 ⋅ 
 0.672454 0.111463 0.15696

julia> parent(T) .= 1
3×3 Matrix{Float64}:
 1.0 1.0 1.0
 1.0 1.0 1.0
 1.0 1.0 1.0

julia> T
3×3 LowerTriangular{Float64, Matrix{Float64}}:
 1.0 ⋅ ⋅ 
 1.0 1.0 ⋅ 
 1.0 1.0 1.0

```

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 2, 2025, 12:54pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/5 "2025-05-02T12:54:05Z")

</div>

Note that `fill!(parent(T), 1)` is likely to be even faster

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 2, 2025, 1:33pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/6 "2025-05-02T13:33:54Z")

</div>

I think they end up being the same. The `@code_native` output looks identical.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 2, 2025, 2:05pm UTC](https://discourse.julialang.org/t/how-to-set-all-elements-in-a-lower-triangular-matrix/128603/7 "2025-05-02T14:05:46Z")

</div>

Yes, this broadcast operation is reduced to a `fill!`. The latter might be faster to compile, though, but the performance will be identical.
