Given two float matrices in R, I would like to get the relative error between every entry, and then among all errors search the max of all of them and compare it against 10%, i.e. MAX_ERROR <= 10%
I know that the raltive error is (for each entry):
|v_ij(MATRIX1) - v_ij(MATRIX2)| / | v_ij(MATRIX1)|
How to do this in R, avoiding a for loop?
If you want to handle cases where the matrix has zeroes in it (which otherwise leads to division by zero), this answer has some solutions: Do you reassign == and != to isTRUE( all.equal() )?
A slight modification of the
almostEqualfunction I suggested there would yield:Note that this calculates the relative error slightly differently than your definition: it’s symmetric and handles small values – and it’s a bit slower because of it…