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

1 view (last 30 days)
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

Answers (1)

Stephen23
Stephen23 on 17 Jul 2019
doc break

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!