Nested while loop problem

6 visualizaciones (últimos 30 días)
Aik Khim Tan
Aik Khim Tan el 24 de Mzo. de 2016
Respondida: Walter Roberson el 24 de Mzo. de 2016
Hi,
I have a test matrix [1 2 3; 4 5 6; 7 8 9] which I provide as input to a function called sandbox. The function is included below:
function [matchFound] = sandbox(input)
i=1; j=1; matchFound=[];
while (i<=size(input,1)) && (isempty(matchFound))
while (j<=size(input,2)) && (isempty(matchFound))
if input(i,j)==5
matchFound=1;
end
j=j+1;
end
i=i+1;
end
end
When I set i=1 in the second line the output is matchFound=[]; but when I set i=2 the output becomes matchFound=1. I have no clue why this happens. Could someone please help?
Best wishes
  1 comentario
Walter Roberson
Walter Roberson el 24 de Mzo. de 2016
It is recommended that you do not name a variable "input" as doing so interferes with the MATLAB function input()

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Mzo. de 2016
You did not reset j to 1 when you start the next iteration of "i".

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by