Why do the MEAN and STD functions return the wrong result for large single precision matrices in any release?

1 view (last 30 days)
If I execute the following commands:
dummy = zeros( 1024, 'single' ) + 200;
mean(dummy(:))
I receive the following result:
ans =
197.1200
But I expected to receive:
ans =
200

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The incorrect result is due to a limitation of floating point arithmetic. The term 'flint' is often used to describe a floating-point number whose value is an integer.
Floating-point operations on flints do not introduce any roundoff error, as long as the results are not too large. For example, addition, subtraction, and multiplication of flints produce the exact result.
The largest flint in single precision is 2^24 = 16777216; as long as operates remain underneath the limit then errors such as this will not occur. In this case, the problem is caused by performing a sum over 1 million elements.
One possible workaround is to execute the following:
dummy = zeros( 1024, 'single' ) + 200;
mean(mean(dummy))
This will perform summations for 1024 element at a time and single precision is able to handle it.
Another workaround you may consider is an iterative refinement scheme like similar to ANOVA1 in the Statistics Toolbox.
dummy = zeros( 1024, 'single' ) + 200;
me = mean(dummy(:))
me = mean(dummy(:)-me) + me
me = mean(dummy(:)-me) + me

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!