help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if statement to find values meeting certain conditions


From: fi
Subject: Re: if statement to find values meeting certain conditions
Date: Thu, 29 Aug 2019 15:50:17 +0200
User-agent: Mutt/1.5.24 (2015-08-30)

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.

it seems your logic is inspired by some spreadsheet calculator. 

It may help to build your data into different variables (num1(:,15)
and num1(:,18) actually seem to be vectors of different semantic). 

Have you tried to vectorize your operations? This may reduce the code
complexity and also the execution time. If you are interested in sign
changes you could try something like

s = sign(x); % x could be your num1(15)
d = diff(s); 
n = find(d); % at this index positions you have sign changes in x



Best Regards

Torsten 


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

-- 
------------------------------------------------------------------------
Dr.-Ing. Torsten Finke
address@hidden
------------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]