If/while loop to break or continue

조회 수: 3 (최근 30일)
jacob gandrup
jacob gandrup 2019년 3월 27일
댓글: Adam Danz 2019년 3월 27일
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