For Loop "Array indices must be positive integers or logical values"

2 ビュー (過去 30 日間)
IDN
IDN 2022 年 9 月 9 日
回答済み: Image Analyst 2022 年 9 月 9 日
Good Morning all,
I have the below statement and its not working when i swith signs and i dont understand why. Appreciate the help!
THIS WORKS
for k = 2:nmr
if a(k) > a(k-1) & a(k-1) > a(k-2) & b(k) < b(k-1) & b(k-1) < b(k-2)
result(k) = 1;
elseif result(k)==0
end
end
BUT Once i swith logical signs i get the matlab error
THIS DOESNT WORK
for k = 2:nmr
if a(k) < a(k-1) & a(k-1) < a(k-2) & b(k) > b(k-1) & b(k-1) > b(k-2)
result(k) = 1;
elseif result(k)==0
end
end

採用された回答

David Hill
David Hill 2022 年 9 月 9 日
matlab starts indexing at 1, you cannot index into an array at 0
for k = 2:nmr
if a(k) < a(k-1) & a(k-1) < a(k-2) & b(k) > b(k-1) & b(k-1) > b(k-2)
b(k-2) is b(0) when k==2 which cannot happen, matlab will error.
  3 件のコメント
IDN
IDN 2022 年 9 月 9 日
Got it thanks!
David Hill
David Hill 2022 年 9 月 9 日
You could start your loop at 3.
for k = 3:nmr
or do something about the b(k-2)
for k = 2:nmr
if a(k) < a(k-1) & a(k-1) < a(max(1,k-2)) & b(k) > b(k-1) & b(k-1) > b(max(1,k-2))

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 9 月 9 日

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by