changing loop index within the loop

2 views (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!