MATLAB: Subscript indices must either be real positive integers or logicals

MATLABsubscript indices

I got this error although it is real positive.
Error in max_nr_method (line 35) frfCurve(location(i))
I printed the value of location(i) right before the error. It is 1002016.
The error is not about the value being huge, because it was greater in the previous iteration.
Thanks in advance.

Best Answer

You are computing location(i) and your computation is resulting in something that is not exactly an integer. For example if you had
x = 0:0.01:2;
location = 1 + x * 100;
then even though algebraically you would expect location to be exactly 1, 2, 3, ... up to 201, because 0.01 is not exactly representable in binary floating point, after multiplication by 100 you only get approximately the integers.
If you have code anything like this, you should avoid the problem. Make the integer form the primary form and calculated the non-integer form, instead of making the non-integer form the primary form and calculating what you expect to be integers.
Second choice: round() or floor() or ceil() the calculated location. If you are doing anything like histogramming, floor() is usually the better choice.