MATLAB: Does ind2sub not return integer output when the datatype of the second argument is single

ind2subMATLABsingle

Why does ind2sub not return integer output when the datatype of the second argument is single?
For example,
 
>> format long
>> A = rand(5860,5091);
>> [i, j] = ind2sub(size(A), single(16779030));
>> i
i =
1849
>> j
j =
2.864000244140625e+03
>> A(i,j)
Subscript indices must either be real positive integers or logicals.

Best Answer

The issue here is with the input 16779030.
 
>> flintmax('single')
ans =
single
16777216
>> single(16779030)
ans =
single
16779030
>> single(16779029)
ans =
single
16779028
 
16779030 happens to be perfectly representable, but 16779029 is not. So minor operations, especially computing things like integer remainders, may result in precision loss.
 
It is a bad idea to use single precision to represent a linear index in a matrix if the total number of elements in the matrix exceeds flintmax(‘single’).  This means it won’t be possible to represent every position in the matrix.