# if statement to find values meeting certain conditions Classic List Threaded 4 messages Open this post in threaded view
|

## if statement to find values meeting certain conditions

 Hello, I have a 200 by 18 array (called num1) and am trying to find some values that meet certain criteria. What I am hoping to do is  1. Starting from i=1, If num1(i, 15) is positive  2. starting j=i,see if num1 (j, 18) is  positive. If negative, increase i by 1 and repeat.  3. If positive, increase j by 1 until num1 (j, 18) is negative . Let's call this num (m, 18)  4. Record values in other columns at rows j and m.  5. The next scan starts from i=m until length(num1) is complete. Below is what I have. Say num1 was positive at (2, 18) and turns negative at (5, 18), instead of keeping only Rows 2 and 5 for the buydate, buyprice, selldate, sellprice, the code records values at rows 2,3,4, 5 and moves on. Please let me know where in the loop is not coded correctly. for i=i:length(num1)   if num1(i, 15)>0  ##15 = 120 day derivative %     j=i     for j=j:length(num1)       if num1 (j, 18)>0         m=j+1;         for m=m:length(num1)           if num1(m,18)<0             buydate(k,1)=num1(j,1); ## 1 = date             buyprice(k,1)=num1(j,5); ## 5 = close price             selldate(k,1)=num1(m,1); ## 1 = date             sellprice(k,1)=num1(m,5); ## 5 = close price             k=k+1;             i=m+1;             break;           else             m=m+1;           endif         endfor         break;       else         j=j+1;       endif     endfor   endif endfor -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: if statement to find values meeting certain conditions

 On Thu, Aug 29, 2019 at 8:28 AM dkimia1001 <[hidden email]> wrote:Hello, I have a 200 by 18 array (called num1) and am trying to find some values that meet certain criteria. What I am hoping to do is  1. Starting from i=1, If num1(i, 15) is positive  2. starting j=i,see if num1 (j, 18) is  positive. If negative, increase i by 1 and repeat.  3. If positive, increase j by 1 until num1 (j, 18) is negative . Let's call this num (m, 18)  4. Record values in other columns at rows j and m.  5. The next scan starts from i=m until length(num1) is complete. Below is what I have. Say num1 was positive at (2, 18) and turns negative at (5, 18), instead of keeping only Rows 2 and 5 for the buydate, buyprice, selldate, sellprice, the code records values at rows 2,3,4, 5 and moves on. Please let me know where in the loop is not coded correctly. for i=i:length(num1)   if num1(i, 15)>0  ##15 = 120 day derivative %     j=i     for j=j:length(num1)       if num1 (j, 18)>0         m=j+1;         for m=m:length(num1)           if num1(m,18)<0             buydate(k,1)=num1(j,1); ## 1 = date             buyprice(k,1)=num1(j,5); ## 5 = close price             selldate(k,1)=num1(m,1); ## 1 = date             sellprice(k,1)=num1(m,5); ## 5 = close price             k=k+1;             i=m+1;             break;           else             m=m+1;           endif         endfor         break;       else         j=j+1;       endif     endfor   endif endfor -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html Did you try single stepping through the code to see where it goes wrong?-- DAS I did and was still unable to debug myself. What I did notice though was that let's say the m was found, and  I made the i=m+1 such that next scan starts from then next row from the last row . I see that the i is still the previous i when a new scan starts (it finally becomes m+1 after the next loop). I'm thinking the scan is exiting from the wrong loop, but don't know where that is exactly. On Thu, Aug 29, 2019 at 9:09 AM Doug Stewart <[hidden email]> wrote:On Thu, Aug 29, 2019 at 8:28 AM dkimia1001 <[hidden email]> wrote:Hello, I have a 200 by 18 array (called num1) and am trying to find some values that meet certain criteria. What I am hoping to do is  1. Starting from i=1, If num1(i, 15) is positive  2. starting j=i,see if num1 (j, 18) is  positive. If negative, increase i by 1 and repeat.  3. If positive, increase j by 1 until num1 (j, 18) is negative . Let's call this num (m, 18)  4. Record values in other columns at rows j and m.  5. The next scan starts from i=m until length(num1) is complete. Below is what I have. Say num1 was positive at (2, 18) and turns negative at (5, 18), instead of keeping only Rows 2 and 5 for the buydate, buyprice, selldate, sellprice, the code records values at rows 2,3,4, 5 and moves on. Please let me know where in the loop is not coded correctly. for i=i:length(num1)   if num1(i, 15)>0  ##15 = 120 day derivative %     j=i     for j=j:length(num1)       if num1 (j, 18)>0         m=j+1;         for m=m:length(num1)           if num1(m,18)<0             buydate(k,1)=num1(j,1); ## 1 = date             buyprice(k,1)=num1(j,5); ## 5 = close price             selldate(k,1)=num1(m,1); ## 1 = date             sellprice(k,1)=num1(m,5); ## 5 = close price             k=k+1;             i=m+1;             break;           else             m=m+1;           endif         endfor         break;       else         j=j+1;       endif     endfor   endif endfor -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html Did you try single stepping through the code to see where it goes wrong?-- DAS 