Why 2 vectors are not the same in matlab?

In summary: This will give you an error if the two floating point numbers do not compare within the specified tolerance.
  • #1
Nate Duong
126
3
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
  • #2
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 Mark44
  • #3
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 Nate Duong and berkeman
  • #4
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)
 
  • #5
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.
 
  • #6
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 berkeman

1. Why do two vectors with the same values appear different in MATLAB?

Two vectors may appear different in MATLAB because they can have different dimensions or data types. MATLAB is case sensitive, so if one vector is defined as a row vector and the other as a column vector, they will appear different even if they have the same values.

2. Can two vectors be considered equal in MATLAB?

Yes, two vectors can be considered equal in MATLAB if they have the same dimensions and all elements have the same values. MATLAB has a built-in function "isequal" that can be used to check if two vectors are equal.

3. Why does the "==" operator give different results for two vectors in MATLAB?

The "==" operator in MATLAB compares the values of two vectors element by element. If the dimensions or data types of the vectors are different, the operator will give different results. To compare if two vectors are equal, the "isequal" function should be used instead.

4. How can I check if two vectors have the same length in MATLAB?

To check if two vectors have the same length in MATLAB, the "length" function can be used. This function returns the number of elements in a vector. If the lengths of two vectors are the same, they will have the same number of elements and the function will return the same value for both vectors.

5. What should I do if I want to compare two vectors in MATLAB, but I am not sure if they have the same dimensions?

If you are not sure if two vectors have the same dimensions, it is best to use the "isequal" function in MATLAB to compare them. This function will check both the dimensions and values of the vectors to determine if they are equal. If you only want to compare the values of the vectors, you can use the "isequaln" function, which ignores differences in dimensions and compares only the values of the vectors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top