14

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.

MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
stanigator
  • 10,768
  • 34
  • 94
  • 129
  • 2
    This question is pretty old, but mathematically you would want to use `norm(A-B)/norm(A)`, not the solutions below. – rlbond Sep 22 '15 at 21:36

6 Answers6

13

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(:))
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
5

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.

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
4

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) );
Edric
  • 23,676
  • 2
  • 38
  • 40
  • 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. – stanigator Jun 02 '09 at 17:28
1

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.

petrichor
  • 6,459
  • 4
  • 36
  • 48
0

For matrices x and y containing floating point values, you may check if array elements are within a given tolerance of one another. Sample code:

tol = 0.05;

result = abs(x - y) <= tol;
Dr Nisha Arora
  • 632
  • 1
  • 10
  • 23
-2

make use of 'isequal(a,b) where a and b are two matrices, if 1 it is true