Ignoring when indexing value positions are invalid

5 Ansichten (letzte 30 Tage)
Morten Jørgensen
Morten Jørgensen am 1 Apr. 2019
Beantwortet: Jan am 1 Apr. 2019
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 Kommentare
KSSV
KSSV am 1 Apr. 2019
If o is < 25, you will get indices negative.......so the error.
Morten Jørgensen
Morten Jørgensen am 1 Apr. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 1 Apr. 2019
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 am 1 Apr. 2019
"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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by