changing loop index within the loop

7 ビュー (過去 30 日間)
Zaharadeen Yusuf
Zaharadeen Yusuf 2021 年 2 月 18 日
コメント済み: Zaharadeen Yusuf 2021 年 2 月 20 日
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.
  1 件のコメント
Zaharadeen Yusuf
Zaharadeen Yusuf 2021 年 2 月 20 日
Ok I will try

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by