5

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..

The Singularity
  • 2,428
  • 3
  • 19
  • 48
Wingmore
  • 139
  • 7

2 Answers2

5

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

BatWannaBe
  • 4,330
  • 1
  • 14
  • 23
1

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
phipsgabler
  • 20,535
  • 4
  • 40
  • 60
Bill
  • 5,600
  • 15
  • 27