Question: 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.

Answer: 
Modifying [Edric's](https://stackoverflow.com/questions/938260/comparing-two-matrices-in-matlab/938474#938474) 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](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isfinite.html) function to ignore those values by setting them to 0.

I wouldn't suggest using [IMAGESC](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imagesc.html) 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.
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.