MATLAB: If statement

if statement

I have a 5X2 matrix its values are:
-1.0000 1.0000
-1.0000 1.0000
-1.0000 -3.0000
-3.0000 -1.0000
-1.0000 -3.0000
but when i put in the statement below i get the response 'no', can someone tell me why, pretty pleas
if (final(1,:) == final(2,:))
display('yes')
else
display('no')
end

Best Answer

There are two problems with your code:
  1. "==" doesn't work well with floating point precision (EDIT: understatement)
  2. "if (final(1,:) == final(2,:))" should read "if all(final(1,:) == final(2,:))"
--- EDIT ---
Make sure you read the link provided by Walter and Floating points by Cleve Moler. The simple rule is: never test if two floating point values are equal.
--- EDIT ---
The internal representation and what is displayed differ. With Matlab the display format can be controlled with the function, FORMAT. With format('hex') the full internal precision is shown. Try
>> v1 = 1/3;
>> v2 = 0.333;
>> v3 = 0.3333;
>> format('hex')
>> v1
v1 =
3fd5555555555555
>> v2
v2 =
3fd54fdf3b645a1d
>> v3
v3 =
3fd554c985f06f69
>>
>> format('short')