Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two matrices in Matlab

I have two matrices x and y, both are results from different algorithms/routines that are supposed to calculate the same result. While I know that the isequal() would check if x and y are the same matrix, the entries in those matrices would not be exactly the same (i.e. some entries may be with 5% off in worst case scenario). In this scenario, what would be the best method of comparing them to see if they are close enough to be considered the same result? Thanks in advance for the advices.

like image 786
stanigator Avatar asked Jun 02 '09 07:06

stanigator


People also ask

How do you compare the differences between two matrices?

If both matrix elements and size are equal, then it displays that the two matrices are equal. If size of matrix is equal but the elements are not equal, then it displays that the matrix can be compared but is not equal. If the size and elements are not matched, then it displays that the matrices cannot be compared.

How do you check if two matrices are similar in Matlab?

tf = isequal( A,B ) returns logical 1 ( true ) if A and B are equivalent; otherwise, it returns logical 0 ( false ). See the Input Arguments section for a definition of equivalence for each data type.

How do you compare elements in a matrix in Matlab?

Accepted Answer One of the ways to do this is to create a matrix C containing the maximum of A and B (element by element) and then checking if this new matrix is same as A or not.

Are two matrices the same Matlab?

Answers (1) 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.


1 Answers

Try this:

tf = abs((A-B)./B)<0.05

This will return a logical matrix which will be true for each element if the relative difference between A and B with respect to B is less than 5 percent.

If you want to ask if all of these are true (they all satisfy the above condition):

all(tf(:))
like image 112
Andrew Shepherd Avatar answered Sep 20 '22 18:09

Andrew Shepherd