'Simplify Julia tuple creation
i need to convert
2-element Vector{Matrix{Tuple{Real, Real}}}:
[(1, 2) (1.8, 2.1) (3, 2)]
[(1, 3) (2.2, 2.9) (3, 3)]
into
(Vector{Real}[[1, 1.8, 3], [1, 2.2, 3]], Vector{Real}[[2, 2.1, 2], [3, 2.9, 3]])
For that my naive approach is
a=[[(1,2) (1.8,2.1) (3,2)],[(1,3) (2.2,2.9) (3,3)]]
b=([first.(s)|>vec for s in a],[last.(s)|>vec for s in a])
Is there a way to write expressions like this simpler (i.e. without repeating most of the
[somefunc.(s)|>vec for s in a]expression)?Is this an efficient solution for
acontaining >1e6 elements in both vectors?
Solution 1:[1]
To get the first element in the tuple you want a way could be
map(i -> reshape(getindex.(i, 1), :), v)
So then
Tuple(map(i -> reshape(getindex.(i,j),:), v) for j in 1:2)
Should give you the output you want, I am not sure this is too much nicer.
Performance is the same but I can run it within a second for 1.e6 in my laptop so it should be okay unless you have really large vectors.
EDIT: I had written 8 in place of 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
