Using multiple conditions with findfirst() in Julia

699 Views Asked by At

Say I have two arrays like so:

  • x = [10, 30, 50, 99, 299]
  • y = [3, 29, 30, 23, 55]

How can I find the index where both following conditions are satisfied?

  • x > 80 & y > 30

So for my example, I expect the return would be index 5. I imagine the format would look something like this:

findfirst(x -> x > 80 \union y -> y> 30, x,y)

but this doesnt work..

Also in my case x and y are columns in a dataframe, but doing an index search also doesnt work..

2

There are 2 best solutions below

1
On BEST ANSWER

Broadcasting seems to work: findfirst((x .> 80) .& (y .> 30))

0
On

Use zip:

julia> x = [10, 30, 50, 99, 299]
5-element Vector{Int64}:
  10
  30
  50
  99
 299

julia> y = [3, 29, 30, 23, 55]
5-element Vector{Int64}:
  3
 29
 30
 23
 55

julia> z = collect(zip(x, y))
5-element Vector{Tuple{Int64, Int64}}:
 (10, 3)
 (30, 29)
 (50, 30)
 (99, 23)
 (299, 55)

julia> findfirst(xy -> first(xy) > 80 && last(xy) > 30, z)
5