# 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:** 13

<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 11, 2020, 3:12pm UTC](https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169/13 "2020-06-11T15:12:29Z")

</div>

Just for completeness, `deleteat!` is quite efficient and accepts a collection for indices. The indices of the diagonal elements are simply `1:n+1:n^2`. Compared to manual loops, it can even be 30% faster.

```julia
A = randn(1000,1000);

@btime offdiag($A); # with bounds checking turned off
  3.399 ms (2 allocations: 7.62 MiB)

@btime deleteat!(vec($A), 1:size(A,1)+1:size(A,1)^2);
  2.595 ms (8 allocations: 7.63 MiB)

```

---

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