For loop mistake?

Hi All,

I have been trying to run this code, but I believe there must be something wrong going on in the for loop part, and I would be grateful for anyone who can catch the mistake. I am not sure what I am missing.

I have two sets of data called rawwave3, rawwave5. Both have the same number of points and basically represent X, and Y values. I have a predetermined set of X-which I must compare with these two waves in the following manner. These predetermined X-values are in the wave called NextWave. The conditions are as follows,

For each Xvalue > the xvalue in NextWave, my code should must print out the Xvalues where Y value < 1. 1 is stored as a variable. The code I have is given below. My major difficulty is in using the index that comes out of the first step where we determine all the Xvalue > the xvalues in NextWave, and matching those indices with that of the Yvalue waves to basically print the Yvalues.

 
for(k=0; k<numpnts(NextWave); k+=1)
     print NextWave[k]
     for(j=0; j<numpnts(rawwave3; j+=1) //Goes through Xvalue wave
         if(rawwave3[j] > NextWave[k])
            print rawwave3[j] //Compares each particular Xvalue with element of NextWave and if greater prints it out
//It is here I need help. I need to find a way to match the index of the elements found satisfying the condition above.
//Since rawwave3, and rawwave5 have the same number of elements, I can then take that matched index to do the step below.
//I am stuck here.
            if(rawwave5[j] < crossing0)
              Yvalue = rawwave5[j]
              print Yvalue
           endif
        endif
     endfor
endfor
                       
If the code is exactly what you are using, you have a typo at the second for ... statement. Here is a condensed version. Otherwise, I am confused what you are trying to achieve.

variable npNW, npRW3, k, j
npNW = numpnts(NextWave)
npRW3 = numpnts(rawwave3)
for (k=0; k < npNW; k+=1)
   for (j = 0; j < npRW3; j+=1)
     if ((rawwave3[j] > nextwave[k]) && (rawwave5[j] < crossing0))
         // do what you need here
     endif
   endfor
endfor


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
[quote=jjweimer]If the code is exactly what you are using, you have a typo at the second for ... statement. Here is a condensed version. Otherwise, I am confused what you are trying to achieve.

Thank you for the help. I'm finding it difficult to exactly describe the purpose of my code. I can try explaining in detail once again. My data points are allotted in the following waves

XValues, YValues, Crossings0, Crossings1, ....

Xvalues are stored in rawwave3
Yvalues are stored in rawwave5
Crossings denotes files with the xvalue locations where each yvalue is crossed throughout the entire wave. The numbers at the end 0, 1, represent the index of the y-values in rawwave5, for identification sake.

Now the calculation I have to make is such that for each x in Crossings0, Crossings1...:

(1) for all x' > x, find the yvalues < 1, along with their corresponding xvalues
(2) for all x' > x, find the yvalues > 9, along with their corresponding xvalues

The values 1 and 9 are just set constants, so I denoted them under variables crosssing0, and crossing1.

I fixed the code like you said, for (1) , but am just having trouble being efficient in processing the data with it:

 
variable npNW, npRW3
npNW = numpnts(NextWave)
npRW3 = numpnts(rawwave3)

//Make /O Yvalueundercrossing, Xvalueundercrossing
//I get that the data must be getting overwritten with each iteration, and was wondering the best way to fix it.

for(k=0; k<npNW; k+=1)
   for(j=0; j<npRW3; j+=1)
          if((rawwave3[j] > nextwave[k]) && (rawwave5[j] < crossing0))
                Yvalueundercrossing[count0] = rawwave5[j]
                Xvalueundercrossing[count0] = rawwave3[j]
                count0 += 1
          endif
  endfor
endfor
  Redimension /N=(count0) Yvalueundercrossing, Xvalueundercrossing





Between x-values, y-values, and crossings as well as the actions that you want to do with them, I am seriously confused.

Can you illustrate what you want in a better way? Perhaps you could make a graphical representation of the data and lines+marks that show what you want. Or give a set of arrays with numerical values.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
It would appear that the redimension operation should be moved as is done here:

for(k=0; k<npNW; k+=1)
   for(j=0; j<npRW3; j+=1)
          if((rawwave3[j] > nextwave[k]) && (rawwave5[j] < crossing0))
                Yvalueundercrossing[count0] = rawwave5[j]
                Xvalueundercrossing[count0] = rawwave3[j]
                count0 += 1
                        Redimension /N=(count0) Yvalueundercrossing, Xvalueundercrossing
          endif
  endfor
endfor
 


As written in your example, the size of the output waves will not change while the for loops are running. I don't think this is the behavior you desire.

The code, as revised in this note, will add a new row each time a crossing is found, this will result in one extra row when execution is finished, but will solve the problem of overwriting the previous result. You should be able to avoid the extra row with a bit more thought.

By the way, I think I see what you are trying to do with the crossings, but the purpose of nextwave is not clear. Maybe you can add an explanation of that.
jjweimer wrote:
Between x-values, y-values, and crossings as well as the actions that you want to do with them, I am seriously confused.

Can you illustrate what you want in a better way? Perhaps you could make a graphical representation of the data and lines+marks that show what you want. Or give a set of arrays with numerical values.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville


Hi JJ, I'm sorry for the hassle, but here you go, I have attached a picture that shows you the arrays that I'm working with.

Basically,

-NumLevelCrossing indicates the number of times the particular y value is crosssed in the entire wave, described by the XValues, and YValues.

-You can ignore Column 5 All Crossings.

-Crossings0, Crossings1... Crossings 6 and so on and so forth, basically describe the locations of each crossing for each particular Yvalue. So Crossing0 describes, the '3' Crossings at Xvalue location in terms of their points: 0, 10, and 29.

(I used FindLevels to find these locations, if I want them in terms of the XValues (column 2) instead of points, should I add /P ? )

-My calculation involves two parts:

For each x-value seen in each Crossings0, Crossings1 ... Crossings 6 file, I have to calculate such that

(1) for all x' > x, find the y-values (from YValues) < 1, and note them down along with corresponding x-values (from XValues).

(x' is just a parameter to denote any other x value > the particular x value chosen,
so if i say x = 0, then all x values above it , in my particular sample will have to be considered)

(2) for all x' > x, find the yvalues > 9, along with their corresponding xvalues

The values 1 and 9 are just set constants, so I denoted them under variables crosssing0, and crossing1, in my code.

This is my full code so far. The commented section is something that I tried to see if it works:

 
Function SplittingProbabilityNumerator4(rawwave3, rawwave5, rawwave6)
      Wave rawwave3 //Xvalues
      Wave rawwave5 //Yvalues
      Wave rawwave6 //NumLevelCrossing
   
    Variable crossing0 = 1
    Variable crossing1 = 9
   
    Variable count0 = 0
   
    Variable i
    Variable j
    Variable k
   

    String List = WaveList("Crossings*", ";", "")
    for (i=0; i<ItemsinList(List); i+=1)
        String NextString = StringFromList(i, List)
        wave NextWave = $NextString //Creates a local reference to an existing wave
        print NameofWave(NextWave) //do something with NextWavve
               
        variable npNW, npRW3
        npNW = numpnts(NextWave)
        npRW3 = numpnts(rawwave3)

        Make /D/O Yvalueundercrossing, Xvalueundercrossing  
        for(k=0; k<npNW; k+=1)
                  for(j=0; j<npRW3; j+=1)
                    if((rawwave3[j] > nextwave[k]) && (rawwave5[j] < crossing0))
                             Yvalueundercrossing[count0] = rawwave5[j]
                            Xvalueundercrossing[count0] = rawwave3[j]
                             count0 += 1
                                Redimension /N=(count0) Yvalueundercrossing, Xvalueundercrossing
                   endif
                 endfor
            endfor
    endfor 

End

//Make /D/O Yvalueundercrossing, Xvalueundercrossing  
//string Yvalueundercrossing
//string Xvalueundercrossing
//for(k=0; k<npNW; k+=1)
   //for(j=0; j<npRW3; j+=1)
                //Yvalueundercrossing = "Yvalueundercrossings" + num2str(k)
        //Xvalueundercrossing = "Xvalueundercrossings" + num2str(k)
            //make/O/N= $Yvalueundercrossing
        //make/O/N $Xvalueundercrossing
        //wave destwave1 = $Yvalueundercrossing
        //wave destwave2 = $Xvalueundercrossing
        // if((rawwave3[j] > nextwave[k]) && (rawwave5[j] < crossing0))
                    //Yvalueundercrossing[count0] = rawwave5[j]
                       //destwave1 = rawwave5[j]
                    //destwave2 = rawwave3[j]
                    //Xvalueundercrossing[count0] = rawwave3[j]
                    //count0 += 1
              //endif
          //endfor
        //endfor
Table.JPG