changing loop index within the loop

7 vues (au cours des 30 derniers jours)
Zaharadeen Yusuf
Zaharadeen Yusuf le 18 Fév 2021
Commenté : Zaharadeen Yusuf le 20 Fév 2021
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

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by