Change loop conditions when condition passed
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi everyone,
I'm trying to modify this loop for when an elseif condtion is executed ONCE, another condtion will be passed.
Basically, tf_r1 is a logic matrix that goes like this: 0 0 1 0 0, after my code hits 1, I want it to recongize the upcoming 0 as something else.
for k=1:5
if tf_r1(k) == 0
strcat('SF in Row 1 Column', num2str(k))
elseif tf_r1(k) == 1
strcat('NA in Row 1 Column', num2str(k))
% When the above elseif is executed ONCE, then execute this:
tf_r1(k) == 0
strcat('TA in Row 1 Column', num2str(k))
end
end
Thank you!
-DP
0 comentarios
Respuesta aceptada
David Hill
el 11 de Jun. de 2020
I misunderstood you.
flag=0;
for k=1:5
if tf_r1(k) == 0 && ~flag
strcat('SF in Row 1 Column', num2str(k))
elseif tf_r1(k) == 1
strcat('NA in Row 1 Column', num2str(k))
flag=1;
elseif tf_r1(k)==0 && flag
tf_r1(k) == 0
strcat('TA in Row 1 Column', num2str(k))
end
end
Más respuestas (1)
David Hill
el 11 de Jun. de 2020
Just add a flag
flag=0;
for k=1:5
if tf_r1(k) == 0
strcat('SF in Row 1 Column', num2str(k))
elseif tf_r1(k) == 1
strcat('NA in Row 1 Column', num2str(k))
% When the above elseif is executed ONCE, then execute this:
if ~flag
tf_r1(k) == 0
strcat('TA in Row 1 Column', num2str(k))
flag=1;
end
end
end
1 comentario
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!