```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: BUG???: 'Sum' depends on the order of values in an array
Date: Wed, 29 Jun 2011 20:35:25 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 106
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1309379725 18346 172.30.248.38 (29 Jun 2011 20:35:25 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 29 Jun 2011 20:35:25 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:734248

Got this peculiar problem that sum (and also prod) appears to depend on the order of the values in the array that is supposed to be summed up.

Consider this code:
-----------
clear

% indeces 1:
ii1=[    21    29    27    25    22    38    23    28    35     1];
% indeces 2, the 3rd and last index changed positions
ii2=[    21    29     1    25    22    38    23    28    35    27];

% read out the values from the array using the indeces:
val1=array(ii1);
val2=array(ii2);

% make sure, all val1 values appear also in val2:
ismember(val1,val2)
% although val1 and val2 have exactly the same values, their sum is different:
sum(val1)==sum(val2)
% not so, if val1 and val2 are sorted first:
sum(sort(val1))==sum(sort(val2))

Output:
ans =
1     1     1     1     1     1     1     1     1     1

ans =
0

ans =
1

-----
Any ideas what's going on?  I use the following Matlab version
-------------------------------------------------------------------------------------
MATLAB Version 7.10.0.499 (R2010a)
Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
-------------------------------------------------------------------------------------
MATLAB                                                Version 7.10       (R2010a)
Bioinformatics Toolbox                                Version 3.5        (R2010a)
Control System Toolbox                                Version 8.5        (R2010a)
Curve Fitting Toolbox                                 Version 2.2        (R2010a)
EzyFit Toolbox                                        Version 2.30
Global Optimization Toolbox                           Version 3.0        (R2010a)
Image Processing Toolbox                              Version 7.0        (R2010a)
MATLAB Report Generator                               Version 3.8        (R2010a)
Optimization Toolbox                                  Version 5.0        (R2010a)
Partial Differential Equation Toolbox                 Version 1.0.16     (R2010a)
Signal Processing Blockset                            Version 7.0        (R2010a)
Signal Processing Toolbox                             Version 6.13       (R2010a)
Statistics Toolbox                                    Version 7.3        (R2010a)
Symbolic Math Toolbox                                 Version 5.4        (R2010a)

Thanks a bunch!

Dave

PS: here is the content of the array:

ans =

1.0e+002 *

1.761055200000000
0
2.421266700000000
0
0
0
0
0
0
0
0
0
0
0
0
0
0.710371200000000
0
1.030091900000000
1.150269500000000
1.290426000000000
1.470684200000000
0.570214700000000
1.370589100000000
1.130840700000000
0
1.280949700000000
1.130840700000000
1.310404900000000
1.140429300000000
0
0.970527700000000
1.280585800000000
1.561011200000000
0.870320300000000
1.010476800000000
0
0.990684200000000
```