Why does ind2sub not return integer output when the datatype of the second argument is single?

2 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Dec 2016
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.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!