Continue in For loop

조회 수: 5 (최근 30일)
Mert ÖZKAYA
Mert ÖZKAYA 2021년 11월 22일
댓글: Mert ÖZKAYA 2021년 11월 22일
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"

채택된 답변

Yusuf Suer Erdem
Yusuf Suer Erdem 2021년 11월 22일
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
Mert ÖZKAYA
Mert ÖZKAYA 2021년 11월 22일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by