Continue in For loop

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by