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

7 次查看(过去 30 天)
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!

采纳的回答

Image Analyst
Image Analyst 2021-11-13
编辑:Image Analyst 2021-11-13
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 个评论
Image Analyst
Image Analyst 2021-11-13
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 2021-11-13
编辑:Waseem AL Aqqad 2021-11-13
It works! Thank you so much.
I added flag as a tag to this question.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2021-11-13
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 个评论
Waseem AL Aqqad
Waseem AL Aqqad 2021-11-13
编辑:Waseem AL Aqqad 2021-11-13
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Results, Reporting, and Test File Management 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by