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:
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.