Comparing two matrices in Matlab

32,471

Solution 1

Modifying Edric's solution:

absTol = 1e-3;   % You choose this value to be what you want!
relTol = 0.05;   % This one too!
absError = x(:)-y(:);
relError = absError./x(:);
relError(~isfinite(relError)) = 0;   % Sets Inf and NaN to 0
same = all( (abs(absError) < absTol) & (abs(relError) < relTol) );

The variable same will be false if either the absolute or the relative error of any element is larger than whatever tolerances you choose. Also, if any elements of x happen to be exactly 0, then some of the elements of relError could end up being either infinite or not-a-number, so I used the ISFINITE function to ignore those values by setting them to 0.

I wouldn't suggest using IMAGESC to compare plots, since 1) the data is scaled when it is displayed, 2) the colormap for the display has a discrete number of color values (which I think is 256 by default, hence lots of rounding), and 3) subtle variations in color may not be all that apparent from visual comparison of two plots.

Solution 2

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(:))

Solution 3

I would consider doing something like this with an absolute tolerance as well as a relative tolerance:

function same = tol( x, y )
absTol = 1e-3;
relTol = 0.05;
errVec = abs( x(:) - y(:) );
same = all( (errVec < absTol) | (errVec./x(:) < relTol) );

Solution 4

When you have very small value pairs in x and y, the result would return 0 although the values are ignorable themselves. So, an addition to the accepted solution

relError(x < absTol) = 0;

might be used to discard very small errors. Thus, the relative error is not considered for these values.

Share:
32,471
stanigator
Author by

stanigator

Updated on July 21, 2022

Comments

  • stanigator
    stanigator almost 2 years

    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.

  • bastijn
    bastijn almost 15 years
    a better answer of what I ment indeed.
  • Jim
    Jim almost 15 years
    Don't you need an absolute value here somewhere?
  • stanigator
    stanigator almost 15 years
    I tried both Edric's and Andrew's method. Even though I got an output from the function being false, when I compared the two results visually by plotting them using imagesc() function, they look very similar. Hence I'm still not too sure what to take of this.
  • Andrew Shepherd
    Andrew Shepherd almost 15 years
    Yep, I've added it now. Thanks.