How might I terminate a for loop based on an if statement?

조회 수: 2 (최근 30일)
Jack McCaslin
Jack McCaslin 2019년 7월 17일
답변: Stephen23 2019년 7월 17일
I'm quite new to MATLAB and I believe this is a fairly simple question but I am hoping someone could help me with how to terminate this for loop when the condition of the if statement inside the loop becomes true? i.e. if a = [2,2,3,2] and b = [2,1,1,1] how might I be able to go about terminating the loop early so that numMatch = 1 when the loop has ended instead of having numMatch = 3?
a = [2,2,3,2]
b = [2,1,1,1];
numMatch = 0;
for (n = 1:4)
if (guess(1) == answer(n))
numMatch = numMatch + 1;
end
end

답변 (1개)

Stephen23
Stephen23 2019년 7월 17일

카테고리

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