Hello everyone,
I have a question which seems to be kind of related to Convert array of tuples to vector.
Edit : I just realized I can do these things with StructArrays except for the tuple case.
However, I wonder if there is different way this can be done.
Let me give a MWE.
julia> struct Input
a
b
end
julia> struct Output
a
b
end
julia> function f1(inp :: Input, x)
return x*inp.a, x*inp.b
end
f1 (generic function with 1 method)
julia> function f2(inp :: Input, x)
return OutPut(x*inp.a, x*inp.b)
end
julia> inp = Input(2.0,3.0)
Input(2.0, 3.0)
julia> f1(inp, 10)
(20.0, 30.0)
julia> f2(inp, 10)
OutPut(20.0, 30.0)
julia> v = 1:5
1:5
Now, if I input vector v to f1 I get
c =f1.(Ref(inp), v)
10-element Array{Tuple{Float64,Float64},1}:
(2.0, 3.0)
(4.0, 6.0)
(6.0, 9.0)
(8.0, 12.0)
(10.0, 15.0)
Question 1:
How can I get the the first, or second element of each tuple into a vector?
julia> c[:][1]
(2.0, 3.0)
which is not what I want.
StructArrays does not seem to help here.It gives the same result.
Now, I can do the following:
julia> get_a(op :: Output) = op.a
get_a (generic function with 1 method)
julia> d =f2.(Ref(inp), v)
10-element Array{Output,1}:
Output(2.0, 3.0)
Output(4.0, 6.0)
Output(6.0, 9.0)
Output(8.0, 12.0)
Output(10.0, 15.0)
julia> get_a.(d)
10-element Array{Float64,1}:
2.0
4.0
6.0
8.0
10.0
This works great.
I was wondering if there was another way to achieve something similar.
I hoped that @unpack from Parameters.jl could do something like
julia> @unpack a, b .= d
ERROR: LoadError: Expression needs to be of form `a, b = c`
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] @unpack(::LineNumberNode, ::Module, ::Any) at /home/omerchiers/.julia/packages/Parameters/l76EM/src/Parameters.jl:743
in expression starting at none:1
but unfortunately it doesn’t work.
Question 2:
Again, with StructArrays I can achieve my goal.
But still I was wondering if there is some way to use @unpack like that?
Thanks in advance,
Olivier