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

2 Ansichten (letzte 30 Tage)
Jack McCaslin
Jack McCaslin am 17 Jul. 2019
Beantwortet: Stephen23 am 17 Jul. 2019
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

Antworten (1)

Stephen23
Stephen23 am 17 Jul. 2019

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by