Why 2 vectors are not the same in matlab?

Click For Summary
SUMMARY

The discussion centers on the comparison of two vectors in MATLAB, specifically a and b, where a = 1.0e-04*[0.11, 0.14, 0.14, 0.14] and b = [0.000011, 0.000014, 0.000014, 0.000014]. Despite appearing similar, the command a == b returns a logical array of zeros, indicating inequality. This discrepancy is attributed to floating-point arithmetic errors, where values are stored imprecisely. The recommended solution is to compare floating-point numbers using a tolerance, as outlined in MATLAB's documentation.

PREREQUISITES
  • Understanding of MATLAB syntax and operations
  • Familiarity with floating-point arithmetic and its implications
  • Knowledge of MATLAB's eps function for tolerance comparison
  • Basic concepts of numerical precision and rounding errors
NEXT STEPS
  • Learn how to implement tolerance-based comparisons in MATLAB
  • Explore MATLAB's format long command for displaying precision
  • Investigate the effects of floating-point representation on numerical calculations
  • Study MATLAB's documentation on numerical precision and data types
USEFUL FOR

MATLAB users, data analysts, and engineers dealing with numerical computations who need to understand floating-point precision issues and effective comparison techniques.

Nate Duong
Messages
125
Reaction score
4
I have 2 vectors:

a = 1.0e-04*[0.110000000000000 0.140000000000000 0.140000000000000 0.140000000000000];
b = [0.000011000000000 0.000014000000000 0.000014000000000 0.000014000000000];


by look at by eyes, it seems similar; but when I use this command:

a == b

then I got:

ans =
1×4 logical array
0 0 0 0


The expectation is:

ans =
1×4 logical array
1 1 1 1


Does anyone knows what wrong with this command in the matlab?

Thank you.
 
Physics news on Phys.org
Nate Duong said:
I have 2 vectors:

a = 1.0e-04*[0.110000000000000 0.140000000000000 0.140000000000000 0.140000000000000];
b = [0.000011000000000 0.000014000000000 0.000014000000000 0.000014000000000];


by look at by eyes, it seems similar; but when I use this command:

a == b

then I got:

ans =
1×4 logical array
0 0 0 0


The expectation is:

ans =
1×4 logical array
1 1 1 1


Does anyone knows what wrong with this command in the matlab?

Thank you.
Rounding error? What happens if you show the values of the a vector after the multiplication is done? Can you try a shift right by 4 places instead of using the multiplication?
 
  • Like
Likes   Reactions: Mark44
Nate Duong said:
I have 2 vectors:

a = 1.0e-04*[0.110000000000000 0.140000000000000 0.140000000000000 0.140000000000000];
b = [0.000011000000000 0.000014000000000 0.000014000000000 0.000014000000000];


by look at by eyes, it seems similar; but when I use this command:

a == b

then I got:

ans =
1×4 logical array
0 0 0 0


The expectation is:

ans =
1×4 logical array
1 1 1 1


Does anyone knows what wrong with this command in the matlab?

Thank you.
It's possible that after multiplying each element of a by 10^(-4), what is actually stored in a is slightly different from the numbers stored in b. For example, the first element of a might be something like 0.000011000000002 .
Floating point arithmetic is notorious in this way. Some decimal value are stored exactly, such as 2.5, .75, but others, such as 0.1 are stored in only an approximate form.
 
  • Like
Likes   Reactions: Nate Duong and berkeman
berkeman said:
Rounding error? What happens if you show the values of the a vector after the multiplication is done? Can you try a shift right by 4 places instead of using the multiplication?
@berkeman: if I shift right by 4 places instead of using the multiplication, I will have what I want, that is vector [1 1 1 1], that is no surprise, but the thing I am working on is very small number (in nano values 1e-9) and sometime MATLAB gives me a vector, example: a = 1.0e-04*[ 1 1 1 1] and sometimes a = [0.0001000000 , 0.0001000000, 0.0001000000, 0.0001000000] , that make me confuse and also think about very small numbers are exist (maybe in pico)
 
Mark44 said:
It's possible that after multiplying each element of a by 10^(-4), what is actually stored in a is slightly different from the numbers stored in b. For example, the first element of a might be something like 0.000011000000002 .
Floating point arithmetic is notorious in this way. Some decimal value are stored exactly, such as 2.5, .75, but others, such as 0.1 are stored in only an approximate form.
@Mark44: that could be, i am happy if some very small decimal number exist (my expectation in nano or pico values) but is there any way to detect them (if they are exist), even I used command format long and still do not see any change.
 
try:
Code:
>> a - b

Also, the MATLAB documentation suggests (under 'eq, ==') a way that avoids the problem when trying to compare floating point numbers:
Compare floating-point numbers using a tolerance, tol , instead of using ==.
Code:
tol = eps(0.5)
abs(C-0) < tol
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K