フィルターのクリア

Nested loop error and saving result

2 ビュー (過去 30 日間)
klb
klb 2020 年 7 月 2 日
コメント済み: klb 2020 年 7 月 2 日
Getitng an " Index in position 1 is invalid. Array indices must be positive integers or logical values." error. What am I not doing right in this loop?
i = 0
results = [];
quantity=35;
for p = 1:quantity-2
totalp = 0.23.*p + 4.0
for q = p+1: quantity - 1
totalq = 0.91.*q + 1.3
for r = q+1: quantity
totalr = 0.30.*r + 3.3
total = totalp + totalq + totalr;
results(i,:) = [p,q,r,total]; %error is here: " Index in position 1 is invalid. Array indices must be positive integers or logical values."
i = i +1;
end
end
end
results
I am working with matrices only for my work. Thanks for your time in advance.

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 2 日
You start i = 0. You do not increment i until after you store a value. So the first time through, you are storing into results(0,:) . Which is not a valid location. Indices in MATLAB must start at 1.
You should move the
i = i +1;
to just before the assignment.
  1 件のコメント
klb
klb 2020 年 7 月 2 日
Thank you Walter. That solves it!

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

その他の回答 (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