Differences between two vectors that suppose to be equal

2 views (last 30 days)
Does anyone know why those are not all equal?
a=.1:.1:.9; b=[.1 .2 .3 .4 .5 .6 .7 .8 .9];
a==b
1 1 0 1 1 1 1 1 1
a(3)-b(3)
5.551115123125783e-17

Accepted Answer

dpb
dpb on 12 Dec 2013
  2 Comments
Felipe Arteaga
Felipe Arteaga on 12 Dec 2013
From the link above:
The difference is that 0:0.1:0.4 increments by a number very close to but not exactly 0.1 for the reasons mentioned below. So after a few steps it will be off whereas [0 0.1 0.2 0.3 0.4] is forcing the the numbers to their proper value, as accurately as they can be represented anyway.
Matt Kindig
Matt Kindig on 12 Dec 2013
To piggyback off this answer, try the following:
format hex
a=.1:.1:.9; b=[.1 .2 .3 .4 .5 .6 .7 .8 .9];
[a', b']
This displays the variable in hex format, which is a representation of the internal binary format that Matlab uses to store the value. Comparing across rows, you can see that the 3rd row differs in the last digit between a and b. Hence the reason a(3) ~= b(3).

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!