From: Sean Douglas on 13 Jul 2010 11:33 "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i1hvq0$fg5$1(a)fred.mathworks.com>... > "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i1hv26$q04$1(a)fred.mathworks.com>... > > hey guys, i have a question regarding if statments and indexing. > > Can i use an if statement to effect indexing. i have code that works if i make an index with one of the options below, but if I make an if statment it does not seem to work, so i am wondering if i can even do what i did below? > > > > if (file1==1) > > jj=(i+1) > > jjj=(p+1) > > elseif(file1==-1) > > jj=(i) > > jjj=(p) > > end > > > > thank you very much > > If you want that sort of control over the indexing, you should try a while loop. > > But why don't you post your whole loop, not just the if statement? And if you explain what it is you're actually trying to do, there may be a better way than what appears to be a triple for loop. hey andy , it did not even cross my mind, that i was making a triple loop. The code is quite wrong, but here some more of it. if (file1==1) jj=(i+1) jjj=(p+1) elseif(file1==-1) jj=(i) jjj=(p) end while(m+p<=n) for i =m+p:n; if((s(i)/GA(i)-s2(i)/GA2(i)>spread1)&&(flag==1)) sharesS2=s(jj)/s2(jj); sharesS2vec1(end+1)=sharesS2; buy= s2(jj) ; buys2plot(end+1)=s2(i); axbuys2plot(end+1)=i; spentbuyS2=sharesS2*buy; short=s(jj); xx=1; flag=-1; dagger=1; elseif...... %% this is really long i have many different conditions.... I hope this shows what im up to .. but basically i am testing strategies on a time series, and sometimes i just want to change everything to 1 day ahead. ( i have indexes using i and p) I already made a file with everything one day ahead and it works, but i would like to just merge both ideas into one file, and this is why i thought to use an if statement. thanks
From: Andy on 13 Jul 2010 11:44 Okay, ignore a lot of my previous posts. You're not making a triple loop, I just assumed you were because you were using multiple standard looping index variables. What you're doing looks mostly correct. (That is, the basic structure of using a while loop and directly controlling your indexing variables is correct. I don't really see what you're actually doing, so I can't comment on correctness.) But you haven't shown several important lines. Are the variables i, p, jj, jjj initialized before the code you've shown? Are these variables and the variable m updated within the while loop? What SPECIFICALLY is wrong with your code? If it's causing an error, you should copy the error here for us to look at (in addition to the lines of code which cause the error). If the code just doesn't calculate what you want it to, then you need to explain more about what you're trying to accomplish and what is not happening correctly in this code. P.S. i and j are both used in MATLAB as the square root of -1, so it's inadvisable to use them as looping variables. Also, you appear to have a variable i defined before you enter the while loop, and then you use i again for the for loop inside the while loop. This is unlikely doing what you intend it to do.
From: someone on 13 Jul 2010 12:40
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i1i0ro$n94$1(a)fred.mathworks.com>... > > The above is correct MATLAB code. > > It should do EXACTLY what you have programmed it to do. > > I don't think so. In a for loop, MATLAB controls the looping variable. E.g. There is nothing mentioned in the OP about a for loop. I simply pointed out that the original code had the correct syntax. It does EXACTLY what it was programmed to do which may not necessarily be what the programmer intended. > > for ix=1:10 > if ix==5 > ix=9; > else > ix > end > end > > Even though it looks like it might print out 1, 2, 3, 4, 9, 10, in fact it will print out 1, 2, 3, 4, 6, 7, 8, 9, 10. > > What was given in the OP is correct MATLAB code. But under the assumption it is inside a for loop, it likely doesn't do what it's intended to do. |