julia> A = [[1,[2,3]], [4,[5,6]], [7,[8,9]]]
3-element Vector{Vector{Any}}:
[1, [2, 3]]
[4, [5, 6]]
[7, [8, 9]]
julia> B = [A[i][2][1] for i in 1:length(A)]
3-element Vector{Int64}:
2
5
8
julia> C = [A[i][2][2] for i in 1:length(A)]
3-element Vector{Int64}:
3
6
9
Is there a cleaner way to extract B and C vectors out than a loop or just writing them out? Ie, my code looks like this:
B = [A[i][2][1] for i in 1:length(A)]
C = [A[i][2][2] for i in 1:length(A)]
D = ...
I really did read a bunch of questions on similar problems but didn’t manage to understand. I was about to put the comprehension in a function then loop over that, but then thought there has to be a way I am missing.
Do you have a meaningful name for what that second element represents? Or better yet, what its first/last components are? Use those names as functions:
frombulator(x) = x[2][1]
proznicator(x) = x[2][2]
Then your B and C are broadcasts:
B = frombulator.(A)
C = proznicator.(A)
Of course this works best if you have good meaningful names you can give these operations, and that’s often the hardest part of most programming.
Can be more involved to grasp the concept at first, but becomes very convenient after that.
Btw, if you care about performance, it could be useful to rethink the data structure. Elements in your A array aren’t type stable, notice the Any in 3-element Vector{Vector{Any}}.