Ignoring when indexing value positions are invalid
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!