Why am I getting unexpected results while working with functions such as 'mean' for array containing large numerical values?
Input these commands in the MATLAB 2019a
>> test = [2^1000, 2^1000];
>> testMean = mean(test);
Now, if testMean is actually the mean of test, then subtracting it from test and taking the mean a subsequent time, should result in 0.
>> zeroCenteredTest = test-testMean;
>> zeroCenteredTestMean = mean(zeroCenteredTest);
You will find zeroCenterTestMean is in fact 0. This case works correctly. However, let me demonstrate a case that does not work correctly.
Let me generate a large 1D matrix with a large amount of large values
>> test2 = [];
>> for i=1:1000
test2(i) = 2^i;
end
>> test2Mean = mean(test2);
If this mean was calculated correctly, the result should be 0.
>> zeroCenteredTest2 = test2-test2Mean;
>> zeroCenteredTest2Mean = mean(zeroCenteredTest2);
You will find in this case, the zeroCenteredTest2Mean is -1.66545893749512e+283 which is definitely not or even close to 0.
Best Answer