changing loop index within the loop

8 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by