Continue in For loop

5 visualizaciones (últimos 30 días)
Mert ÖZKAYA
Mert ÖZKAYA el 22 de Nov. de 2021
Comentada: Mert ÖZKAYA el 22 de Nov. de 2021
Hello,
I'm trying to make a for loop something like this. However the output is not like that I want.
Isn't it should be like:
1ler
1ler
0lar
1ler
But Matlab's output is just
1ler
Why it doesn't continue to the iteration?
A=[1 0 1 0 0 0 1];
for i=1:2:A(1,end)
if A(1,i)==1
display("1ler")
elseif A(1,i)==0
display("0lar")
else
display("error")
end
end
"1ler"

Respuesta aceptada

Yusuf Suer Erdem
Yusuf Suer Erdem el 22 de Nov. de 2021
Hi Mert. Could you try like below. I just changed "for i=1:2:A(1,end)" into "for i=1:length(A)".
A=[1 0 1 0 0 0 1];
for i=1:length(A)
if A(1,i)==1
display("1ler")
elseif A(1,i)==0
display("0lar")
else
display("error")
end
end
  1 comentario
Mert ÖZKAYA
Mert ÖZKAYA el 22 de Nov. de 2021
Thank you for your answer. Since I need only odd numbers for i value, I changed for i=1:length(A) into i=1:2:length(A).

Iniciar sesión para comentar.

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