Comparing Numbers in different waves

Hi,

This is a very general question. I have three waves, each with a bunch of numbers, like follows (as an example)

Wave1 Wave2 Wave3
1 2 1
2 3 2
3 5 4
4 1 5
5 4 3

I have to do a comparison such that for each value in Wave1,

the first time/occurrence in Wave2 that the value > the chosen value in Wave1, I append it, and
the first time/occurrence in Wave3 that the value > the chosen value in Wave1, I append it, and

and then, using the indices as our references like Index2 (index where Wave2 value > chosen value from Wave1)
and Index3 (index where Wave3 Value > chosen value from Wave1),

write out the following condition that if Index2 > Index3, then print out '1' to be stored in a separate wave, Wave4
and if Index 2 < Index3 print out '0' accordingly stored in Wave4. Therefore Wave4, would just be one with elements
like 10101010 depending on the preceding calculations.

To elaborate further, lets just take the first element of Wave1 = 1, the index where Wave2 value is > 1 for the first time, is 1.
Similarly for Wave3 the index value is 2. Therefore we have Index2 = 1, Index3 = 2 and since Index 2 < Index3 we must print out 0 to Wave4 as our result.

I am stuck on how to implement this in the sense of getting the first time when the values from Wave2, and Wave 3 are greater than the given value from Wave1. So far I was running along the lines of doing an if statement but it hasn't gotten me anywhere. I would be grateful for tips. Should I use a while loop or something?

Function Calculator(Data1, Data2, Data3) //in other words, Wave1, Wave2, and Wave3
    Wave Data1, Data2, Data3
    Variable numPoints = numpnts(Data1)
    Variable i
    Variable count0

    Make /D/O Results
    for(i=1; i<numPoints; i+=1)
        if(Wave2[i] > Wave1[i]) ...