views:

2294

answers:

6

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.

A: 

Easiest way is to make a loop which checks all numbers with a 5% error check build in. So check if X(1,1) = Y(1,1) with a 5% error. But this way your matrix should not be too large and it should just a be one timer, not for a program which has to do it every time :).

bastijn
It's even better to do it without a loop. Andrew Shepherd's suggestion does the same but without any loops.
Nathan Fellman
+9  A: 

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
a better answer of what I ment indeed.
bastijn
Don't you need an absolute value here somewhere?
Jim
Yep, I've added it now. Thanks.
Andrew Shepherd
+3  A: 

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
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
A: 

Andrew: don't forget the absolute value!

tf = abs(A-B)./B<0.05
Ben
It should probably be "tf = abs((A-B)./B) < 0.05", since B itself could technically have negative values.
gnovice
Fixed now. (D'oh!) :-)
Andrew Shepherd
+3  A: 

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.

gnovice
A: 

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.

İsmail Arı