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

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

Accepted Answer

Image Analyst
Image Analyst on 13 Nov 2021
Edited: Image Analyst on 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 Comments
Image Analyst
Image Analyst on 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 on 13 Nov 2021
Edited: Waseem AL Aqqad on 13 Nov 2021
It works! Thank you so much.
I added flag as a tag to this question.

Sign in to comment.

More Answers (1)

Jan
Jan on 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 Comment
Waseem AL Aqqad
Waseem AL Aqqad on 13 Nov 2021
Edited: Waseem AL Aqqad on 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

Sign in to comment.

Categories

Find more on Results, Reporting, and Test File Management in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!