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

6 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 13 de Nov. de 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 comentario
Waseem AL Aqqad
Waseem AL Aqqad el 13 de Nov. de 2021
Editada: Waseem AL Aqqad el 13 de Nov. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Results, Reporting, and Test File Management en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by