# How to assign the diagonal elements of part of matrix to a value?

**URL:** https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243
**Category:** General Usage
**Tags:** question, matrices
**Created:** [October 22, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243 "2021-10-22T21:33:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [October 22, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/1 "2021-10-22T21:33:21Z")

</div>

I know that `diagind()` returns the index of the diagonal elements of matrix, in which we can assign them to a specific value such as in the below

```julia
aa=rand(9,9);
aa[diagind(aa)] .= 1

```

However, lets say I am working on a part of the matrix `aa` such as `aa[5:7,5:7]` and I want to assign its diagonal elements to 1 (in other words aa[5,5]=1, aa[6,6]=1, aa[7,7]=1), how can I do that?

---

<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: [October 22, 2021, 11:46pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/2 "2021-10-22T23:46:35Z")

</div>

You can simply do:

```julia
aa[diagind(aa)[5:7]] .= 1

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [October 25, 2021, 1:33pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/3 "2021-10-25T13:33:00Z")

</div>

Thank you very much!  
@Seif_Shebl  
How is about changing the diagonal elements of `aa[5:7,4:6]` (i.e., aa[5,4]=1, aa[6,5]=1, aa[7,6]=1)  
I tried `aa[diagind(aa)[5:7,4:6]] .= 1` but it gives an error

```julia
ERROR: BoundsError: attempt to access 9-element StepRange{Int64, Int64} at index [5:7, 5:7]
Stacktrace:

```

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [October 25, 2021, 10:13pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/4 "2021-10-25T22:13:34Z")

</div>

Not very elegant but you could do a [comprehension](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions):

```julia
  [aa[ind...] = 1 for ind in zip(5:7,4:6)]

```

It could be very clearly (and with better performance) written with a for loop as well.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 25, 2021, 10:17pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/5 "2021-10-25T22:17:45Z")

</div>

> [@aramirezreyes](#):
>
> Not very elegant but you could do a [comprehension](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions):

Never use a comprehension when you just want a `for` loop. A comprehension generates an array, so if you don’t want this array it is a pointless side effect, and it’s not any less compact to write `for ind in zip(5:7,4:6); aa[ind...] = 1; end` or `for j in 4:6; aa[j+1,j] = 1; end`

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [October 25, 2021, 10:18pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/6 "2021-10-25T22:18:17Z")

</div>

Thanks for noting it!

---

<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: [October 25, 2021, 10:22pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/7 "2021-10-25T22:22:00Z")

</div>

`diagind` takes an extra argument, so you could write

```julia
jl> aa[diagind(aa, -1)[4:6]] .= 1

```

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [October 25, 2021, 10:29pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/9 "2021-10-25T22:29:15Z")

</div>

@DNF Great  
How is about  
How is about changing the diagonal elements of `aa[1:3,4:6]` (i.e., aa[1,4]=1, aa[2,5]=1, aa[3,6]=1)

---

<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: [October 25, 2021, 10:34pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/10 "2021-10-25T22:34:53Z")

</div>

```julia
jl> aa[diagind(aa,3)[1:3]] .= 1;

```

Various combinations of this is possible. Just try some things.

Another approach is to use an extra step involving a `view`:

```julia
jl> av = view(aa, 5:7, 4:6);

jl> av[diagind(av)] .= 1;

```

And then you could take more general and interesting slices as well, like

```julia
jl> av = @view aa[1:2:end, end:-2:1];

jl> av[diagind(av)] .= 1;

jl> aa
10×10 Matrix{Int64}:
 0 0 0 0 0 0 0 0 0 1
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 1 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0

```

---

<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: [October 25, 2021, 10:44pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/11 "2021-10-25T22:44:13Z")

</div>

For speed, you should probably use a loop.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [October 25, 2021, 10:44pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/12 "2021-10-25T22:44:52Z")

</div>

@DNF Yes, actually the speed is critical in my situation

---

<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: [October 25, 2021, 10:52pm UTC](https://discourse.julialang.org/t/how-to-assign-the-diagonal-elements-of-part-of-matrix-to-a-value/70243/13 "2021-10-25T22:52:09Z")

</div>

Then you should try both and benchmark. Sometimes, the simple high-level versions are competitive.
