Change loop conditions when condition passed

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

采纳的回答

David Hill
David Hill 2020-6-11
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

更多回答(1 个)

David Hill
David Hill 2020-6-11
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 个评论
DP
DP 2020-6-11
编辑:DP 2020-6-11
Hi David,
Thank you for your help. I executed the flag you mentioned, but it did not change the response.
Here are the answers:
ans =
'SF in Row 1 Column1'
ans =
'SF in Row 1 Column2'
ans =
'NA in Row 1 Column3'
ans =
logical
0
ans =
'TA in Row 1 Column3'
ans =
'SF in Row 1 Column4'
ans =
'SF in Row 1 Column5'
I'm trying to get the last two answers to be 'TA in Row 1 Column 4' and 'TA in Row 1 Column 5'.
Thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by