If/while loop to break or continue

3 次查看(过去 30 天)
Hello. I have setup a simple data set which illustrates the idea of my code. I have trouble setting up the loop to break/continue the current code. The comments explain the purpose of the code. Don't pay much attention to written for loop etc. its just showing its position in the code.
z = [1; 2; 5; 3; 4; 5]
x = [1 2 1 3 4; 3 2 1 2 1; 1 1 2 3 4;1 2 1 3 4; 3 2 1 2 1; 1 1 2 3 4]
limit = 2;
%%We want to compare z to each coloum of x. If value of z exceeds 5, then compared
%%value of x should be excluded. Which mean we only store compared values
%%when z are below 5.
%%We want to do it with a loop which breaks (remove the compared values
%%above of z) and then continues throught rest of the values. We have an
%%idea that a if or while loop could work.
for L:length(z)
if(x > limit)
break/continue
end
  4 个评论
Luna
Luna 2019-3-27
%% We want to do it with a loop which breaks (remove the compared values
%%above of z) and then continues throught rest of the values
You say we want to remove here. If it is not removed what is the result for the above arrays and while your limit is 2?
You say "If value of z exceeds 5" you don't want to do a calculation, but you look for if x > limit in the code. I really don't get your question.
Adam Danz
Adam Danz 2019-3-27
Sorry, I still don't have a solid understanding of the goal. The break() function terminates execution of a for-loop. So it can't 'break' and 'continue' at the same time.
Perhaps it would be better to explain the main goal of what you're doing. Also how does the 'limit' variable come into play? Is that the threshold of z that should break the loop?

请先登录,再进行评论。

回答(0 个)

类别

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

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by