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

2 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Stephen23
Stephen23 il 17 Lug 2019

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by