How to get a matrix anti-diagonal?

Hello, good afternoon.

How to get an offdiagonal from a square matrix? I have diag function for the diagonal but how to acquire the below?

A = [1 2; 3 4]
offd(A)
result -> [2, 3]

Does diag(A, 1) work?

1 Like

not worked
image

I think you’re after what I’d call the antidiagonal.

You could do something like this:

function antidiag(A::AbstractMatrix)
    allequal(size(A)) || throw(ArgumentError("antidiag requires a square matrix"))
    return A[[CartesianIndex(i,j) for (i,j) in zip(axes(A, 1), reverse(axes(A, 2)))]]
end
1 Like

Yep, I think julia doesnt have a similar function

If you don’t care about allocations, a simple solution might be

julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> diag(rotl90(a))
2-element Vector{Int64}:
 2
 3
1 Like

thats a nice solution