Ignoring when indexing value positions are invalid

5 ビュー (過去 30 日間)
Morten Jørgensen
Morten Jørgensen 2019 年 4 月 1 日
回答済み: Jan 2019 年 4 月 1 日
Hi
I get and error when running this code, is it possible to ignore when position is invalid at just let the code proceed?
so it stille includes as many as possible?
thanks
for o = 1:length(mBall)
if mBall(o,4:5) == 0
mBall(o,6) = 1;
elseif sum(displace(o-25:o+25,1)) < 2
mBall(o,6) = 3;
else
mBall(o,6) = 0;
end
end
%Index in position 1 is invalid. Array indices must be positive integers or logical values.
%Error in Velocity (line 93)
%elseif sum(displace(o-25:o+25,1)) < 2
  2 件のコメント
KSSV
KSSV 2019 年 4 月 1 日
If o is < 25, you will get indices negative.......so the error.
Morten Jørgensen
Morten Jørgensen 2019 年 4 月 1 日
yes I know, but I'm asking if it is possible to ignore this error
so when o = 24, it will only take the 24 first indces into account? instead of evaluating when the negative as well

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

回答 (2 件)

Image Analyst
Image Analyst 2019 年 4 月 1 日
Try this
for k = 1 : length(mBall)
if all(mBall(k,4:5) == 0)
mBall(k,6) = 1;
elseif k >= 26
if sum(displace(k-25 : k+25,1)) < 2
mBall(k,6) = 3;
else
mBall(k, 6) = 0;
end
else
mBall(k,6) = 0;
end
end

Jan
Jan 2019 年 4 月 1 日
"when o = 24, it will only take the 24 first indces into account"
for k = 1:length(mBall)
if mBall(k, 4:5) == 0
mBall(k, 6) = 1;
elseif sum(displace(max(1, k-25):k+25,1)) < 2
mBall(k, 6) = 3;
else
mBall(k, 6) = 0;
end
end
"o" can be confused with "O" or "0", so "k" is used here instead.
You should not "ignore" an error, but create some code to avoid it. Here the index cannot be smaller than 1, so set 1 as minimal index explicitly. Or maybe:
elseif k > 25 && sum(displace(k-25:k+25,1)) < 2

カテゴリ

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