MATLAB: Array Indexing Logical Values

arraysbinary floating point numbersfloating pointindexing

My colleague and I were trying to iterate through an array using time steps. However, on certain time steps and time lengths, we recieve an "Array indexes must be positive indexes or logical values" error.
Why does this error appear at all?
Why does this error only appear sometimes?
We know that rounding the index prevents the error, but the index is already displayed as an integer, so why would that matter? Is it because the index (1e5) is really a decimal? (i.e. Is there a hidden decimal place not being shown to the user that makes the index not an integer?)
Any help is appreciated
Relevant Forum Posts
Code
disp("Notice how these parameters have no error");
stepThroughTime(.0001,3);
disp("While these parameters do");
stepThroughTime(.00001,3);
function stepThroughTime(steps,time)
%This function steps through an array of times
%bugs: certain steps and times result in error
times=0:steps:time;
windowsize=1/steps;
disp(windowsize);
for b=windowsize+1:windowsize:length(times)
x=b-windowsize;
%When x is rounded, there is no error
%x=round(x);
times(x);
end
end

Best Answer

  • 100000
    Array indices must be positive integers or logical values.
    >> x
    x =
    100001
    >> x - floor(x)
    ans =
    0.999999999985448
    >> fprintf('%.99g\n', x)
    100000.999999999985448084771633148193359375
    The problem is that 1/0.00001 is not an integer because 0.00001 is not exactly representable in binary floating point.