Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
BUG???: 'Sum' depends on the order of values in an array

Subject: BUG???: 'Sum' depends on the order of values in an array

From: David

Date: 29 Jun, 2011 20:35:25

Message: 1 of 2

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
load tmptmp1 % loads only the array, nothing else.

% 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)
MATLAB License Number: STUDENT
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)
Simulink Version 7.5 (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)
Spreadsheet Link EX Version 3.1.1 (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

Subject: BUG???: 'Sum' depends on the order of values in an array

From: Roger Stafford

Date: 29 Jun, 2011 21:27:09

Message: 2 of 2

"David" wrote in message <iug2ad$hta$1@newscl01ah.mathworks.com>...
> 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. .......
- - - - - - - - - - -
  You have no reason to suppose that in any computer's calculation of numbers where round off errors can occur, these will automatically give exactly the same net result for different orderings of operations. All you can expect is that each such error is within the range expected of such errors. This is a characteristic of all digital computing, not just matlab.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us