Every now and then I need to find the index of the min/max element of a filtered data structure.
I was wondering, how do you normally tackle this quite common problem?
As a first reaction I think of
findmin(o, filter(f, data)) but filtering doesn’t hold the original indices.
I ended up using
findmin(o, skipmissing([f(d) ? d : misssing for d in data]) where I inject
missing values in the values that should be filtered out.
julia> using Random julia> rng = MersenneTwister(0); julia> data = [(rand(rng), rand(rng)) for _ in 1:10]; julia> dataobjective(d) = d; julia> filterdata(d) = d > 0.5; julia> findmin(dataobjective, skipmissing([filterdata(d) ? d : missing for d in data])) (0.5392892841426182, 6)
It’s very convenient that
skipmissing is propagating the original indices. Is there something like
skipfilter that would do exactly as
skipmissing but for elements satisfying a predicate ?
And how would you solve the above problem ?