So when I calculate the index, it is 1.7290e+03. Calling "index" in the code throws an error, but copying and pasting "1.729e+03" directly into the argument of the cell array does work.
You calculated index is displayed as 1729 but the value stored in the variable is actually very slightly different from 1729.
>> x = 0;
>> for k = 1:10
x = x + 0.1;
end
>> x
x =
1
>> d = x-1
d =
-1.11022302462516e-16
"Ten steps of length t are not precisely the same as one step of length 1. MATLAB is careful to arrange that the last element of the vector 0:0.1:1 is exactly equal to 1, but if you form this vector yourself by repeated additions of 0.1, you will miss hitting the final 1 exactly."
You're doing something similar, expecting your calculations to give an exact integer value but instead getting something that just misses hitting that integer value exactly.
Where possible, avoid computing array indices. If you must, compute using only operations on integer values that must give integer values if at all possible or round if not.
Best Answer