# Off diagonal elements of Matrix

**URL:** https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169
**Category:** General Usage
**Created:** [June 10, 2020, 11:03pm UTC](https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169 "2020-06-10T23:03:22Z")
**Posts on this page:** 1
**Showing post:** 12

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [June 11, 2020, 11:13am UTC](https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169/12 "2020-06-11T11:13:49Z")

</div>

That is true. My code looks more like a C example, while yours is a nice, generic oneliner. And it is easy to turn it into an interator over the off-diagonal indices than allocating a new vector.

```julia
offdiag_iter(A) = (ι for ι in CartesianIndices(A) if ι[1] ≠ ι[2])

```

Thanks!

---

_[View the full topic](https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169)._
