What is difference between isequal and == in comparing two vectors

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)

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

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
That comment showed some character.
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
@Bruno Luong: the first example compares two 1x4 arrays with the same values.
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?
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 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."
Fine, but putting in tripplet they violate the equivalence relation rules
So TMW should not claim using EQUIVALENT word
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...
(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.

Sign in to comment.

Asked:

on 13 Sep 2019

Commented:

on 15 Sep 2019

Community Treasure Hunt

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

Start Hunting!