필터 지우기
필터 지우기

Ignoring when indexing value positions are invalid

조회 수: 4 (최근 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 CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by