Is there an elegant way of extracting the upper triangle of a matrix as a vector using Base? Besides, of course, writing something like
function vec_triu(M::AbstractMatrix{T}) where T
m, n = size(M)
m == n || throw(error("not square"))
l = n*(n+1) ÷ 2
v = Vector{T}(l)
k = 0
for i in 1:n
v[k + (1:i)] = M[1:i, i]
k += i
end
v
end
Not that I know, but just FYI, you need to write out the loop; your function allocates (on 0.7 from this week). This does not even make your code longer or harder to read.
Hi everyone, I needed this functionality a lot recently and extracted my code for this into a separate package (I hope this is not overkill) but I removed the allocations in my implementation.
I hope it helps someone else as well and feedback is much appreciated.