フィルターのクリア

Easy for loop question

3 ビュー (過去 30 日間)
Ron
Ron 2014 年 8 月 18 日
コメント済み: Ron 2014 年 8 月 18 日
I'm trying to make a simple For Loop:
for i=1:603
if A(i,2)>((A(i-1,2)+A(i+1,2))/2)*10
A(i,:)=[];
and i'm getting the error:
"Subscript indices must either be real positive integers or logicals."
I don't understand why, since i is an integer.
Thanks

採用された回答

Adam
Adam 2014 年 8 月 18 日
編集済み: Adam 2014 年 8 月 18 日
i - 1
will be 0 in the first pass through the loop. Indices must be positive integers, i.e. 1 or greater. Matlab indexes arrays from 1, not from 0 as e.g. C++ does.
  1 件のコメント
Ron
Ron 2014 年 8 月 18 日
Thank you !

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

その他の回答 (2 件)

Evan
Evan 2014 年 8 月 18 日
編集済み: Evan 2014 年 8 月 18 日
i must be a real, positive integer. Because i starts at 1, and you reference A(i-1,2), your first iteration will try to access A(0,2), and 0 is not a proper matrix index under MATLAB's conventions.
To solve this, either adjust your indexing so that you access A(i+1,:) or change your range to i = 2:603+1
  1 件のコメント
Ron
Ron 2014 年 8 月 18 日
Thank you !!

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


Guillaume
Guillaume 2014 年 8 月 18 日
As others have said, the issue is from A(i-1,2) that is not valid for i = 1.
There's a second issue that you are deleting rows as you are iterating over them so in all likelyhood, you'll hit an 'index exceeds matrix dimensions' before the end of the loop because you no longer have 603 rows.
  1 件のコメント
Ron
Ron 2014 年 8 月 18 日
You are right !!! thanks a-lot !!!

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by