Clear Filters
Clear Filters

update values ​​in a for loop except one

3 views (last 30 days)
hi everyone, i have the following problem. I have a simulation seconds ranging from 0 to 630 seconds, at which every 90 I would like to perform actions.
now here I would like to obtain a newgreentime that every in every value of t (v) updates according to a function of this type, in practice that takes into consideration one column at a time of the veicoliTT (n, v) to be subtracted from the greentime calculated in the t(v) previous. as soon as I got this value I would like to use it to set it to yet another function. This whole operation should be computed for every v, except for greentime with v = 1, for which I would like to take a constant value outside the for loop instead with the values 45,45,45,45 (while the veicoliTT (n, v) for v = 1 must be equally calculated by the for loop). how can i do this?
L is constant matrix 4x34
Thank you so much
SIM_STEPS = [1 630];
beginTime = SIM_STEPS(1);
duration = SIM_STEPS(2);
endTime = SIM_STEPS(1) + SIM_STEPS(2) - 1;
k=1:floor(endTime/T); %is a vector ranging from 1 to 7
t = T:T:duration; % returns the values 90,180 ... 630
inductionID=traci.inductionloop.getIDList(); % function that creates me a vector of 34 values
T=90;
greentime=[45 45 45 45]'; %I would like it to be the value of newgreentime for v = 1
for i = 1:duration
traci.simulation.step();
for n = 1: length(inductionID)
num(n,i)=traci.inductionloop.getLastStepVehicleNumber(inductionID{n});
% creates a 34x630 vector
end
for v=1:length(k)
if i==t(v)
for n = 1: length(inductionID)
veicoliTT(n,v)=sum(num(n,(t(v)-(T-1)):t(v)));% I create a sum vector of 90 elements for each value of v
newgreentime(v)=newgreentime(v-1)-L*veicoliTT(:,v); % don't work
end
end
end
end

Answers (2)

Benjamin Thompson
Benjamin Thompson on 22 Feb 2022
Not understanding what you are trying to do exactly. But one way to test that a counter has reached some multiple of an integer T is to use the modulus function:
if mod(i, T) == 0
for n = 1: length(inductionID)
veicoliTT(n,v)=sum(num(n,(t(v)-(T-1)):t(v)));% I create a sum vector of 90 elements for each value of v
newgreentime(v)=greentime(v)-L*veicoliTT(:,v); % don't work
end
end
  1 Comment
Marco Carapellese
Marco Carapellese on 22 Feb 2022
hello, thanks for the answer. I had no problem with dividing into 90s intervals as
for v = 1: length (k) and if i == t (v) works. My problem is that within these cycles I would like to calculate (I tried but that part doesn't work) that newgreentime as a negreentime of the previous period -less veicoliTT (considering only the column of it that concerns the v iterate at that moment). I would like to repeat this operation for each v present in the for loop in order to obtain as many newgreentime vectors as there are v. Another exception, however, is that the newgreentime calculated in v = 1 must not follow that function but must be exactly equal to the greentime value abroad of all the cycles and therefore to a constant

Sign in to comment.


David Hill
David Hill on 22 Feb 2022
Edited: David Hill on 22 Feb 2022
If you really want seconds... you might want to use clock and etime. I don't entirely understand what you want. For example something like this:
stime=clock;
itime=stime;
v=0;
count=1;
while etime(stime,clock)*1000<630
traci.simulation.step();
for n = 1: length(inductionID)
num(n,count)=traci.inductionloop.getLastStepVehicleNumber(inductionID{n});
% creates a 34x630 vector
end
count=count+1;
if etime(itime,clock)*1000>90
itime=clock;
v=v+1;
for n = 1: length(inductionID)
veicoliTT(n,v)=sum(num(n,(t(v)-(T-1)):t(v)));% I create a sum vector of 90 elements for each value of v
newgreentime(v)=greentime(v)-L*veicoliTT(:,v); % don't work
end
end
end
  2 Comments
Marco Carapellese
Marco Carapellese on 22 Feb 2022
thank you for the answer, I tried in the answer to the other guy to better explain what my problem is
Marco Carapellese
Marco Carapellese on 22 Feb 2022
definitely command "newgreentime(v)=newgreentime(v-1)-L*veicoliTT(:,v)"
it is badly written for what I intend to do, it was just to give an idea of ​​what I would like to do for all v, except the first one

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!