f

#### Matlab basic operations

```I have recently found some strange behaviour of MatLab and I would appreciate if somebody explained me why it happens and how to overcome this problem.

x1 = [1/3 repmat(2/3,1,10)]';
x2 = [repmat(2/3,1,10) 1/3]';
mean(x1)==mean(x2)

% because

sum(x1)==sum(x2)

I get results false.

Why are the sums not the same although the difference between x1 and x2 is only the order of elements?
```
 0
Justinas
5/21/2010 9:55:20 PM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader.

5 Replies
1286 Views

Similar Articles

[PageSpeed] 52

```Don't feel bad, you are only the 1 millionth person to discover the wonders of floating point arithmetic.  Have a read:

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F
```
 0
Matt
5/21/2010 10:09:04 PM
```Matt Fig wrote:
> Don't feel bad, you are only the 1 millionth person to discover the
> wonders of floating point arithmetic.

(1 millionth to within round-off error, that is.)
```
 0
Walter
5/21/2010 10:18:19 PM
```If you look at the difference of the sums:

sum(x1)-sum(x2)

ans =

8.8818e-016

you can see that this is arithmetic rounding error. Values like 1/3 can't be expressed exactly as a binary expansion, so there are always approximations involved in computations involving such numbers. It's not surprising that the order in which the sums are computed makes a difference - I dare say someone could do a more detailed analysis of why these particular examples come out as they do.

The general message is this: it's not wise to use the == or ~= operators on double values, unless you know for sure that they have been computed in ways that avoid any truncation of a binary representation. The simplest thing is only to use the operators on the results of integer computations - otherwise use >, <=, >= and >, and instead of a==b use abs(a-b)<tol, where tol is an appropriate tolerance which may depend on your computation.
```
 0
David
5/21/2010 10:22:06 PM
```On May 21, 10:55=A0pm, "Justinas " <justinas.bra...@gmail.com> wrote:
> I have recently found some strange behaviour of MatLab and I would apprec=
iate if somebody explained me why it happens and how to overcome this probl=
em.
>
>
> x1 =3D [1/3 repmat(2/3,1,10)]';
> x2 =3D [repmat(2/3,1,10) 1/3]';
> mean(x1)=3D=3Dmean(x2)
>
> % because
>
> sum(x1)=3D=3Dsum(x2)
>
> I get results false.
>
> Why are the sums not the same although the difference between x1 and x2 i=
s only the order of elements?

always test equal like

>> x1 =3D [1/3 repmat(2/3,1,10)]';
>> x2 =3D [repmat(2/3,1,10) 1/3]';

>> if (mean(x1)-mean(x2))<eps
disp('Equal');
else
disp('Unequal');
end
>>>Equal
```
 0
Parker
5/22/2010 9:30:13 AM
```Parker <xenoszh@gmail.com> wrote in message <c753e854-c76d-4f9e-b24b-8a50bf157035@y12g2000vbg.googlegroups.com>...
> ...
> always test equal like
>
> >> x1 = [1/3 repmat(2/3,1,10)]';
> >> x2 = [repmat(2/3,1,10) 1/3]';
>
> >> if (mean(x1)-mean(x2))<eps
> disp('Equal');
> else
> disp('Unequal');
> end

Not quite correct. Try the above with, for example, x2 = ones(size(x1)) and you'll find it will still report 'Equal'. What is needed is to take the absolute value of the difference:

if abs(mean(x1)-mean(x2))<eps

This is only correct if eps is the right tolerance. It often will be appropriate, but not always - it depends on the details of the computation that leads up to the test.
```
 0
David
5/22/2010 11:18:05 AM