How to call functions from inside an if statement only once?

2 visualizzazioni (ultimi 30 giorni)
How can I call both decision and implement functions from inside the if statement just for once, and then call them without using the if statement for the rest of time steps?
N = numnodes(G); % number of nodes in the steady state
[~,M] = initial(~); % attacking the network
% M is the number of inactive nodes
for tt = 2: 50
if any(needRemoveNode)
[~,M] = Stages(needRemoveNode,M); % attacking the network
else
break
end
% Calling the recovering functions
if M(tt) >= 0.3 * N % triggering level (delay response)
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
end
end
Thanks!

Risposta accettata

Image Analyst
Image Analyst il 13 Nov 2021
Modificato: Image Analyst il 13 Nov 2021
Try setting a flag:
N = numnodes(G); % number of nodes in the steady state
[~,M] = initial(~); % attacking the network
% M is the number of inactive nodes
alreadyCalled = false; % Say that decision and implement have not been called yet.
for tt = 2: 50
if any(needRemoveNode)
[~,M] = Stages(needRemoveNode,M); % attacking the network
else
break
end
% Calling the recovering functions
if (M(tt) >= (0.3 * N)) && ~alreadyCalled % triggering level (delay response)
% Calling for the very first time if we get here.
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
elseif alreadyCalled
% Once they've been called already, call them regardless of the "if" test.
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
end
end
  3 Commenti
Image Analyst
Image Analyst il 13 Nov 2021
Sorry, you need to set the flag once it's been called:
N = numnodes(G); % number of nodes in the steady state
[~,M] = initial(~); % attacking the network
% M is the number of inactive nodes
alreadyCalled = false; % Say that decision and implement have not been called yet.
for tt = 2: 50
if any(needRemoveNode)
[~,M] = Stages(needRemoveNode,M); % attacking the network
else
break
end
% Calling the recovering functions
if (M(tt) >= (0.3 * N)) && ~alreadyCalled % triggering level (delay response)
% Calling for the very first time if we get here.
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
alreadyCalled = true; % Set flag to indicate we've called it.
elseif alreadyCalled
% Once they've been called already, call them regardless of the "if" test.
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
end
end
Waseem AL Aqqad
Waseem AL Aqqad il 13 Nov 2021
Modificato: Waseem AL Aqqad il 13 Nov 2021
It works! Thank you so much.
I added flag as a tag to this question.

Accedi per commentare.

Più risposte (1)

Jan
Jan il 13 Nov 2021
I'm not sure if I understand you correctly, but maybe:
for tt = 2: 50
...
if tt > 2 || M(tt) >= 0.3 * N
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
end
end
  1 Commento
Waseem AL Aqqad
Waseem AL Aqqad il 13 Nov 2021
Modificato: Waseem AL Aqqad il 13 Nov 2021
Hi Jan,
initial and stages increase M (No. of inactive routers), and decision and implement decrease M. I'm checking how those two process exist and ineract at the same time. When I call decision and implement without adding a delay, I got the plot as in 1 (attached).
%if M(tt) >= 0.3 * N % triggering level
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
%end
and when I call them after some time, I got the plot as in 2.
if M(tt) >= 0.3 * N % triggering level
[G_dmg, ~] = decision(~);
[G_dmg] = implement(G_dmg, ~);
end
So, to avoid the oscillation in 2, I want to add the delay response only once.
I hope this explains my problem correctly

Accedi per commentare.

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by