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

7 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 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 Kommentar
Waseem AL Aqqad
Waseem AL Aqqad am 13 Nov. 2021
Bearbeitet: Waseem AL Aqqad am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Results, Reporting, and Test File Management finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by