Hi all, I don’t work much with comprehensions of nested loops, but I came across something which is probably obvious but unexpected to me. Why is there a difference between these two approaches to the nested loop?
Without comprehension, I iterate in a nested loop over the rows in the inner loop, as is efficient for column major ordering as in Julia.
a = zeros(4,3)
c = 0
for j in 1:3, i in 1:4
global c+= 1
a[i,j] = c
end
the output is as I expect
Julia> a
4x3 Matrix{Float64}:
1.0 5.0 9.0
2.0 6.0 10.0
3.0 7.0 11.0
4.0 8.0 12.0
But when sticking the above loops in a comprehension, the matrix is transposed, even though it is filled in column major order!
c = 0
a = [(global c+=1) for j in 1:3, i in 1:4]
3x4 Matrix{Int64}:
1 4 7 10
2 5 8 11
3 6 9 12
Why is the matrix transposed in the comprehension?