Asked by Hansori Chang
on 13 Sep 2019 at 21:32

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

Answer by the cyclist
on 13 Sep 2019 at 21:36

Edited by the cyclist
on 13 Sep 2019 at 21:40

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).

Bruno Luong
on 14 Sep 2019 at 11:55

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 at 12:14

(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.

Stephen Cobeldick
on 15 Sep 2019 at 5:31

Another related discussion:

Unfortunately the MATLAB documentation rather misnamed the scalar string "" (the name is so bad I cannot write it here):

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.