result = (A-B)./A*100;
Now I can just use the isfinite function to produce a matrix of logical values that tells me which pixel locations have valid values (simultaneously handling the case of numerical overflow). Granted, this doesn't do much to simplify the code above. We could have just checked beforehand to find the pixels of A that had zero values. However, if you have a complicated expression involving multiple divisions, logarithms, or other functions that are undefined for some portion of the real numbers, treating these situations as "recoverable" in some sense allows you to write cleaner, more readable code if your implementation language permits you to just do the operation and check for NaN values (+Inf and -Inf too) afterward.