changing loop index within the loop

9 次查看(过去 30 天)
Hello, please how can I write a loop that its index value changes within the loop. The function below, receive inputs of arrival time of some packets. The function monitors the system inactivity (i.e time when there is no arrival). if there is arrival, it reset the timer and set a variable u to be active otherwise if after a certain consecutive inactivity the function should set u to inactive and exit.
function [s,u] = Active(simTime, pArrival)
inactivityTimer=1; % initializing timer
inactivityThreshold=4; % initializing timer threshold
for v=inactivityTimer:inactivityThreshold %loop for monitoring the arrival
u='active';
if pArrival(simTime)==1 % checking for arrival in the array which contains random bits
inactivityTimer=0; % Resetting the time when there is arrival
simTime=simTime+1; % inreamenting the simulation time
elseif inactivityTimer>inactivityThreshold % checking if the timer reaches exceeds threshold
break;
else
inactivityTimer=inactivityTimer+1; % incrementing simulation time
simTime=simTime+1;
end
end
if inactivityTimer>inactivityThreshold
u='inactive'; % Setting u to be inactive
end
s=simTime; % Assigning simtime to output variable
end

采纳的回答

James Tursa
James Tursa 2021-2-20
If you need to modify the loop index within the loop, use a while-loop instead of a for-loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by