Checking of equality for two matrices

Hi I have trouble coming out the code to find the equality of two matrices. I have to test and display that the matrices are equal. I am trying to include 'if' statement in as well.
Here is the question:
Calculate AxB and BxA and test if AB = BA.
Thanks!

5 commentaires

Martin C.
Martin C. le 8 Oct 2019
isequal(A,B) should do it
cheers
Ian Thean
Ian Thean le 8 Oct 2019
I am torn between result = A==B and isequal(A,B).
I believe it would help me partly in this coding. Thanks!
Guillaume
Guillaume le 8 Oct 2019
Modifié(e) : Guillaume le 8 Oct 2019
Have you actually tested the difference between the two? It should be very obvious.
A == B will return a matrix the same size as A (and B) telling you which elements of A are equal to the corresponding element of B.
isequal(A, B) will return a single true/false telling you if all the elements of A are the same as the ones in B.
the cyclist
the cyclist le 8 Oct 2019
An advantage of using isequal(A,B) over A==B is that if A and B are different sizes (e.g. 4x4 vs. 3x3), then A==B will throw an error because of the dimension mismatch, but isequal will return a result.
This may not be important in your specific case, but could be in general.
the cyclist
the cyclist le 8 Oct 2019
Also, I think you mean to check isequal(A*B,B*A), not just isequal(A,B).
Be aware of the accuracy of floating-point numbers for exact equality check.

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 8 Oct 2019

1 vote

The difference between the isequal function and the == operator is that the second will work element by element. That means that isequal will tell you if two matrices are the exact same, while == will test all elements individually.
One golden tip: always make sure your input to if or while is a scalar, not a logical array. Array input tend to do something different from what you might mean.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by