From: Steve on
Running Matlab 7.8.0.347 (R2009a) under WinXP on an Intel Core 2 Duo processor, executing the command sum(a(:)), where a is a 304x446x12 single-precision matrix, gives slightly different results when run multiple times. Here's what I'm doing, with the file example.mat that can be downloaded from ftp://ftp.npolar.no/Out/Hudson/

>> clear
>> load example.mat
>> b = permute(a,[3 1 2]);
>> sum(a(:))
ans =
1.7639e+023
>> sum(a(:))
ans =
1.7639e+023
>> sum(a(:))
ans =
1.7638e+023 %Note difference in 5 digit
>> sum(a(:))
ans =
1.7638e+023
>> sum(a(:))
ans =
1.7639e+023
>> sum(b(:)) %Now summing the permuted matrix
ans =
1.7634e+023
>> sum(b(:))
ans =
1.7634e+023
>> sum(b(:))
ans =
1.7639e+023
>> sum(b(:))
ans =
1.7639e+023

When using format long, there appear to be 2 unique results for each sum. I'm not really surprised that sum(a(:))~=sum(b(:)) since I know floating point addition is not associative, but I would expect sum(a(:))==sum(a(:)) and sum(b(:))==sum(b(:)) consistently, at least on the same machine. Can someone please explain why that is not so? Does sum or the : operator take elements in different orders sometimes? Thanks for any help. It's probably not that important, but it seemed a bit strange, and a step beyond the normal rounding error.