From: Sean Douglas on 6 Aug 2010 03:28 Hello all, I have a for loop that seems to be counting by 2’s, I can not figure out why. I have some of the code below, I don’t think the while loop has anything to do with the problem I am having, but I will leave the beginning of it in the code I show you, just in case it is. As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration. xx=0; yy=0; p=0; m=1; flag=1; profit=0; prof=0; while(m+p<=n) for i =(m+p):n; if((s(i)/Mc(i)-s2(i)/Mc2(i)>spread1)&&(flag==1)) sharesS2=s(i)/s2(i); buy= s2(i) ; buys2plot(end+1)=s2(i); axbuys2plot(end+1)=i; spentbuyS2=sharesS2*buy; short=s(i); shorts1plot(end+1)=s(i); axshorts1plot(end+1)=i; sharesS1=1; date1=t(i); datevector1open(end+1)=t(i); datevectorindexo1=i; portvalue(end+1)=-s(i)+sharesS2*s2(i); xx=1; flag=-1; dagger=1; elseif((s2(i)/Mc2(i)-s(i)/Mc(i)>spread1)&&(flag==1)) sharesS2=s(i)/s2(i) ; buy=s(i); buys1plot(end+1)=s(i); axbuys1plot(end+1)=i; sharesS2vec1(end+1)=sharesS2; short=s2(i); shorts2plot(end+1)=s2(i); axshorts2plot(end+1)=i; spentshortS2=sharesS2*short; date2=t(i); datevector(end+1)=t(i); datevectorindexo2=i; xx=1; flag=-1; dagger=2; end if(xx==1) break, %shoots out of next end( for i = m+p:n) end end y=i+1 …………………the while loop continues.... Please help
From: Darren Rowland on 6 Aug 2010 03:44 Hi Sean, Try setting a breakpoint and step through the program while it's executing. This way you can keep track of all variables. Hth Darren
From: Jan Simon on 6 Aug 2010 09:00 Dear Sean, > As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration. I cannot find the "added i" in your code. The FOR loop you have posted increments i by 1. > for i =(m+p):n > % Insert this: disp(i); !!! > ... The intermediate code cannot change the loop counter i > end > y = i+1 I'd never access the loop counter i outside the loop. In former Matlab versions the value of the counters was not well defined. Do you expect "i" to equal "n" after the loop? Then I'd use "n". But of course, the former suggestion of using the debugger hits the point. While the debugger has full access to the variables, values and functions, this newsgroup has just some poor implementations of cristal balls. Kind regards, Jan
From: Sean Douglas on 6 Aug 2010 13:34 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i3h118$3cv$1(a)fred.mathworks.com>... > Dear Sean, > > > As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration. > > I cannot find the "added i" in your code. > The FOR loop you have posted increments i by 1. > > > > for i =(m+p):n > > % Insert this: disp(i); !!! > > ... The intermediate code cannot change the loop counter i > > end > > y = i+1 > > I'd never access the loop counter i outside the loop. In former Matlab versions the value of the counters was not well defined. > Do you expect "i" to equal "n" after the loop? Then I'd use "n". > > But of course, the former suggestion of using the debugger hits the point. While the debugger has full access to the variables, values and functions, this newsgroup has just some poor implementations of cristal balls. > > Kind regards, Jan hey thanks all, can anyone explain what jan means by "using n", does he mean in replace of i? i dont expect i to equal n until the very last iteration. i did some more tests on this code I have posted and it is indeed incrementing by 2.The output is showing that my indexing i’s in this for loop are all even numbers so my program is skipping the evaluation of every other data point. I am trying to solve with the debugger. I can not wait to solve this one.... any ideas? thanks for any help
From: Andy on 6 Aug 2010 14:01 "can anyone explain what jan means by "using n", does he mean in replace of i? i dont expect i to equal n until the very last iteration." Jan's referring to this part of your code: for i =(m+p):n; % snip end y=i+1 The comment was that you should not try to access the loop counter i outside of the loop. It is NOT the case, however, that y will necessarily get set to n here, because of the break. Still, it would be clearer if you did this: for i=(m+p):n % stuff if (x==1) || (i==n) y=i; % set y before you break break end end "I am trying to solve with the debugger. I can not wait to solve this one.... any ideas?" Well, what is the debugger telling you? You should be stepping line by line through the for loop, and you should be able to see the instant i changes in a way that it shouldn't. That should tell what line is the problem, and you should post that information here. We cannot run your code, as there are pieces of data you haven't given to us.
|
Next
|
Last
Pages: 1 2 3 Prev: Thz signal generation using Cmos and linear superimposition Next: read_grib on 64-bit R2009b |