Why i get the out of bound error in this code?

3 ビュー (過去 30 日間)
IBM watson
IBM watson 2018 年 10 月 21 日
コメント済み: IBM watson 2018 年 10 月 21 日
myset=[1 2 3 4 6];
capacity=6;
passedall=[]
passed=[]
for q = 1:5
    q=q+1
    capacity=6;
    passedall = [passedall passed]; 
    myset= setdiff(myset,passedall);
    for b= 1:5
      if (myset(b) < capacity);
        capacity= capacity - myset(b)
        passed(b) = myset(b)
      end
    end
  end

i get this error:

error: myset(4): out of bound 3
error: called from
    deneme at line 13 column 5
  2 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 21 日
provide all the datas
madhan ravi
madhan ravi 2018 年 10 月 21 日
passedall = [passedall passed]
doesn't have any values ?

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

採用された回答

Guillaume
Guillaume 2018 年 10 月 21 日
Why i get the out of bound error in this code
Because by the time b equals 4, you've removed enough elements from myset that there are only 3 left and myset(b) is no longer valid. Have you tried debugging your program?
I have no idea what you're trying to do with your code since it's not commented at all. As it is, it doesn't make any sense and probably your loops are not needed. Without an explanation of what you're trying to achieve we can't tell you how to fix it.
Note that
for q = 1:5
q=q+1
has exactly the same effect as
for q=2:6
  1 件のコメント
IBM watson
IBM watson 2018 年 10 月 21 日
"Because by the time b equals 4, you've removed enough elements from myset that there are only 3 left and myset(b) is no longer valid."
This!! Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by