How can I write a condition statement in for loop?
1 次查看(过去 30 天)
显示 更早的评论
I have this code. I want if the a=>6 the code increase k by one and go back from the beginning of the for loop. how can I do it ?
k=4;
b=[2,5,2,6,3,4,5,9,10,2,9,4,6,8,7,3,4];
c=[5,2,5,6,9,8,3,1,6,5,5,3,6,4,1,6,9];
for i=1:k
a=b(1,i)+c(1,i);
ss(1,i)=a;
if a>6 then % this does not work
k=k+1
end
end
ss
The code I wrote does not work. How can I write this condition ?
0 个评论
回答(1 个)
Image Analyst
2018-2-17
Try this:
kMax = 4;
thisK = 1;
maxIterations = 10000; % Some number larger than you ever expect.
b=[2,5,2,6,3,4,5,9,10,2,9,4,6,8,7,3,4];
c=[5,2,5,6,9,8,3,1,6,5,5,3,6,4,1,6,9];
loopCounter = 1;
while loopCounter <= length(c) && ...
thisK < kMax && ...
loopCounter < maxIterations
a = b(loopCounter) + c(loopCounter);
ss(loopCounter) = a;
if a > 6
thisK = thisK+1
end
loopCounter = loopCounter + 1;
end
ss
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!