|
From: Alexander Karyotakis on 19 Jun 2008 10:40 Hi all, I have a problem figuring out how to simulate the following example: As you can see in the reference example at the end, we have 5 years divided by 2 periods each year. I want to access each period in sequence and get the results. I start by building up a random array for 'ttf' and every period that passes 'ttf' should reduce by 0.5 points. Now every even or odd period 'avail' should take a certain value for each 'apple'. The problem I am having is to link them together. For example, we start with an ‘apple’ having a 'ttf' of 0.6 before the loop, then it enters the first period and is reduced by 0.5 points becoming 'ttf = 0.1', then because it is period number 1, five points should be added becoming ‘ttf = 5.1’. Then as it enters period 2, 0.5 points should be deducted as default becoming ‘ttf = 4.6’, but also because it is the second period 1 point should be deducted from ttf so ‘ttf = 3.6’.(and so forth) I have tried several different ways of simulating this but without success, anyone that could help me spot the mistake? I have tried using the 'if' command but it is giving me error because it does not recognize the 'm=1:2:periods'. years = 5; periods = 2*years; apples = 2; X = rand(apples,1); ttf = -0.5 * (log(X)); for m=1:periods; ttf = ttf -0.5; for m=1:2:periods; for i=1:apples; avail(i) = ttf(i) + 5; end end for m=2:2:periods; for i=1:apples; avail(i) = ttf(i) - 1; end end end
From: French Caro on 19 Jun 2008 10:58 "Alexander Karyotakis" <vamvax7(a)hotmail.com> wrote in message <g3dr4m$eep$1(a)fred.mathworks.com>... > Hi all, > > I have a problem figuring out how to simulate the following > example: > > As you can see in the reference example at the end, we have > 5 years divided by 2 periods each year. I want to access > each period in sequence and get the results. > > I start by building up a random array for 'ttf' and every > period that passes 'ttf' should reduce by 0.5 points. > > Now every even or odd period 'avail' should take a certain > value for each 'apple'. > > The problem I am having is to link them together. For > example, we start with an ‘apple’ having a 'ttf' of 0.6 > before the loop, then it enters the first period and is > reduced by 0.5 points becoming 'ttf = 0.1', then because it > is period number 1, five points should be added becoming > ‘ttf = 5.1’. > > Then as it enters period 2, 0.5 points should be deducted as > default becoming ‘ttf = 4.6’, but also because it is the > second period 1 point should be deducted from ttf so ‘ttf = > 3.6’.(and so forth) > > I have tried several different ways of simulating this but > without success, anyone that could help me spot the mistake? > I have tried using the 'if' command but it is giving me > error because it does not recognize the 'm=1:2:periods'. > > years = 5; > > periods = 2*years; > > apples = 2; > > X = rand(apples,1); > ttf = -0.5 * (log(X)); > > for m=1:periods; > ttf = ttf -0.5; > > for m=1:2:periods; > for i=1:apples; > avail(i) = ttf(i) + 5; > > end > end > > for m=2:2:periods; > for i=1:apples; > > avail(i) = ttf(i) - 1; > > > end > > end > end > Hi, I'm not enough courageous to understand exactly what you want, but this : for m=1:periods; ttf = ttf -0.5; for m=1:2:periods; is not good : you can't use the same iterator for both loops, MATLAB will be lost (as I am). So change one of them (and also the second for m loop after). Caroline
From: Alexander Karyotakis on 20 Jun 2008 07:54 Thanks Caroline, I wanted to change the second and third 'for' loops to 'if' command. But when I use 'if' it gives me an error at this line: if m:1:2:periods; I just want to input the following to the code: if I have periods 1,3,5,7... do this, otherwise if it is period 2,4,6... do something else. But I can't find a way to input that through an IF command. The second and third time I am using the 'for' loop is not the correct way to do it, as you have mentioned. 'IF' command should be used. Thanks again. Alex
From: Gavrilo Bozovic on 20 Jun 2008 08:21 try if m%2 == 1 >yourcommands else >your commands the '=' will affect a value to 'm', losing the latter value! "Alexander Karyotakis" <vamvax7(a)hotmail.com> wrote in message <g3g5op$1rb$1(a)fred.mathworks.com>... > Thanks Caroline, > > I wanted to change the second and third 'for' loops to 'if' > command. But when I use 'if' it gives me an error at this line: > > if m:1:2:periods; > > I just want to input the following to the code: > if I have periods 1,3,5,7... do this, otherwise if it is > period 2,4,6... do something else. > > But I can't find a way to input that through an IF command. > > The second and third time I am using the 'for' loop is not > the correct way to do it, as you have mentioned. 'IF' > command should be used. > > Thanks again. > > Alex
From: French Caro on 20 Jun 2008 08:27
"Alexander Karyotakis" <vamvax7(a)hotmail.com> wrote in message <g3g5op$1rb$1(a)fred.mathworks.com>... > Thanks Caroline, > > I wanted to change the second and third 'for' loops to 'if' > command. But when I use 'if' it gives me an error at this line: > > if m:1:2:periods; > > I just want to input the following to the code: > if I have periods 1,3,5,7... do this, otherwise if it is > period 2,4,6... do something else. > > But I can't find a way to input that through an IF command. > > The second and third time I am using the 'for' loop is not > the correct way to do it, as you have mentioned. 'IF' > command should be used. > > Thanks again. > > Alex OK I got it. 2 ways : One which do exactly what you say if ismember(m,1:2:periods) %do one thing else %do othet thing end The second which is better in my opionion is to verify if m is odd or even : if rem(m/2)==0 %do one thing else %do other thing end Caroline |