简体   繁体   中英

Setting Up an If loop in a For loop

I have two tables set up.

Table 1 = [0, 1, 2, 3, 4, 5]

Table 2 = [554, 543, 554, 544, 564, 553]

All am trying to do is the following:

(1) I want to take the Value in Table 1, read it, and then identify it with the index of the same in Table 2.

So for example Table1[0] = 0, and therefore we would find that Table2[Table1[0]] = 554

(2) From the given value of Table 1, I would then like to go through the entirety of Table 2, and append the value where the Data is first either lesser than A = 541, or greater than B = 553, and store the corresponding value accordingly in a different table.

As I read Table1, I begin with Table1[0] = 0, I then proceed to make this 0 as my index for Table2 and proceed onwards to read the entirety of Table2, stopping and appending whenever the values in Table2 are < A or > B.

For example, starting with Table1[1] = 1 --> I would then proceed to read off all the values following Table2[1]: Table2[2] = 554, ... etc. In this case, the first value after Table2[1] is 554 which is > B so I append it it to a different table and keep going, the next value Table2[3] = 544 which is neither greater than B or less than A so I ignore and proceed etc.

My code is as follows but am not sure what is going wrong. I am using the Igor environment, but the code is pretty much readable like a C++:

Function Trial(Data1, Data2)
//Waves are just the tables that contain the data. Data1 = Table1, Data2 = Table2

Wave Data1 
Wave Data2

Variable A = 541;
Variable B = 553;
Variable i, j, k
Variable XScale, Invalid
Variable numPoints = numpnts(Data1) //Number of Points in the array 
Variable numPoints2 = numpnts(Data2)

for(i=0; i<numPoints; i+=1)
   XScale = Data1[i]
   Wave TrialCrossingA, TrialCrossingB
   Make /N=(numPoints2)/O TrialCrossingA, TrialCrossingB 
   //Makes new tables to append required values 
   for(j=Xscale; j<numPoints2; j+=1)
            If(Data2[j] <= Xa)
                TrialCrossingA[i] = XScale
            Elseif(Data2[j] >= Xb) 
                TrialCrossingB[i] = XScale
            Endif 
   endfor
endfor

End

I think you made a mistake here,

   for(j=Xscale; j<numPoints2; j+=1)
            If(Data2[j] <= Xa) // This is supposed to be Data2 I guess ?
                TrialCrossingA[i] = XScale
            Elseif(Data2[j] >= Xb) // This is supposed to be Data2 I guess ?
                TrialCrossingB[i] = XScale
            Endif 
   endfor

The code does not compile here. The variables Xa and Xb are not defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM