What is difference between isequal and == in comparing two vectors
Show older comments
Two vectors that have the same values and same length.
However, I want to know what are the difference between == and isequal(A,B), comparing two vectors
Answers (1)
the cyclist
on 13 Sep 2019
Edited: the cyclist
on 13 Sep 2019
isequal will return a single logical value, indicating whether or not the two vectors are identical.
== will give a vector with element-by-element comparison.
>> x = [1 2 3 4];
>> y = [1 2 3 5];
>> x == y
ans =
1×4 logical array
1 1 1 0
>> isequal(x,y)
ans =
logical
0
Also, isequal will work on unequal-sized vectors, and == will give an error (because the mismatch in number of elements prevents the element-by-element comparison).
11 Comments
John D'Errico
on 13 Sep 2019
Edited: John D'Errico
on 13 Sep 2019
Good answer, of course. However, I am now sponsoring the vector equality act, stating that ALL vectors should be considered equal under the laws of MATLAB, regardless of class, length, or values. Surely we cannot allow vectors to suffer discrimination merely because of such minor differences.
Under my proposed act, isequal will ALWAYS return true, for all vectors.
ALL vectors should be considered equal under the laws of MATLAB, regardless of class...
Well, as it happens, isequal is class-blind, and that can be a source of confusion and mishap. Not everyone would expect the following result,
>> isequal( [102,114,111,103] , 'frog' )
ans =
logical
1
the cyclist
on 13 Sep 2019
That comment showed some character.
Bruno Luong
on 14 Sep 2019
Edited: Bruno Luong
on 14 Sep 2019
Hmmm all vectors is not equal under the laws of MATLAB, with regard to their class...
>> isequal( [102,114,111,103] , 'frog' )
ans =
logical
1
>> isequal( 'frog' , "frog" )
ans =
logical
1
>> isequal( [102,114,111,103] , "frog" )
ans =
logical
0
The second is really the only irregular one, but apparently in the interest of user convenience the == operator was intentionally overloaded for such cases.
The third example compares a 1x4 array with a scalar array: why should they be equal?
Bruno Luong
on 14 Sep 2019
Edited: Bruno Luong
on 14 Sep 2019
I don't expect they are equal, I juts point out ISEQUAL is not a mathemamatics transitive relation as the name suggests.
If your logic holds I would expect
isequal( 'frog' , "frog" )
returns FALSE. one is 1 x 4 array of char the other is scalar (string) array.
This is one of the apparent inconsistency of MATLAB.
the cyclist
on 14 Sep 2019
Edited: Bruno Luong
on 14 Sep 2019
The second example is actually also comparing a 1x4 character array with a 1x1 string array.
But the documentation explicitly carves out that case:
"String scalars and character vectors containing the same sequence of characters are equivalent."
Bruno Luong
on 14 Sep 2019
So TMW should not claim using EQUIVALENT word
Bruno Luong
on 14 Sep 2019
One more example of inconsistentcy
>> isequal( {'frog'} , "frog" )
ans =
logical
1
>> isequal( 'frog' , "frog" )
ans =
logical
1
>> isequal( 'frog' , {'frog'} )
ans =
logical
0
Scratch my head...
the cyclist
on 14 Sep 2019
Edited: the cyclist
on 14 Sep 2019
(I was replying to Stephen, not you Bruno.)
I agree that the non-transivitity is pretty surprising, and could be documented more clearly. Your latest set of comparisons are really a surprise to me, especially that
isequal( {'frog'} , "frog" )
is true.
Stephen23
on 15 Sep 2019
Another related discussion:
Unfortunately the MATLAB documentation rather misnamed the scalar string "" (the name is so bad I cannot write it here):
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!