update values in a for loop except one
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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
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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!