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

<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, 10:42am UTC](https://discourse.julialang.org/t/off-diagonal-elements-of-matrix/41169/8 "2020-06-11T10:42:28Z")

</div>

Thanks for all the answers so far, @L_Adam, @Tamas_Papp! In the initial post I ordered the matrix entries rowwise, but everybody correctly assumed columnwise ordering 😃

I came up with a quick and dirty way for 1-based indexing and quadratic matrices, which does not need an `if`-statement:

```julia
function offdiag(A::Matrix)
    @assert size(A)[1] == size(A)[2]
    D = size(A)[1]
    v = zeros(D*(D-1))
    for i in 1:D
        for j in 1:(i-1)
            v[(i-1)*(D-1)+j] = A[j,i]
        end
        for j in (i+1):D
            v[(i-1)*(D-1)+j-1] = A[j,i]
        end
    end
    v
end

```

I know from direct SIMD programming that conditions in your code can decrease performance gains from SIMD by a factor (2?). Would this apply here as well? Or is Julia smart enough or not using SIMD at all?

PS: In my code example, one has off course disable bounds checking to make automatic SIMD available.

---

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