Continue in For loop

5 visualizzazioni (ultimi 30 giorni)
Mert ÖZKAYA
Mert ÖZKAYA il 22 Nov 2021
Commentato: Mert ÖZKAYA il 22 Nov 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"

Risposta accettata

Yusuf Suer Erdem
Yusuf Suer Erdem il 22 Nov 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 Commento
Mert ÖZKAYA
Mert ÖZKAYA il 22 Nov 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).

Accedi per commentare.

Più risposte (0)

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by