I am trying to construct a static vector from a struct. So suppose I have
struct TT
a
b
c
end
t = TT(2, 3, 5)
then I can do this using
using StaticArrays
out = SA[t.a, t.c]
Now I want to write a function that takes t
and
ind = [:a, :c]
as input, and returns out
. It should be such that it works as well if for example ind=[:b]
or ind=[:a, :b]
. Any suggestions to this?
You could do this:
julia> SVector(ntuple(i -> getproperty(t, ind[i]), length(ind)))
2-element SVector{2, Int64} with indices SOneTo(2):
2
5
:+1: Thank you!
Frank van der Meulen has marked this topic as resolved.
Last updated: Nov 22 2024 at 04:41 UTC