Comparing two matrix in Matlab
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
1 comentario
Anuj Kumar
el 24 de Dic. de 2020
you can try this,assumming x and y has same dimensions
len=lenght(x);
tolerance=x-y;
check=tolerance<0.001;
sum_Check=sum(check)
if sum_Check==len %then results can be accepted
Respuestas (2)
Titus Edelhofer
el 26 de Jun. de 2014
Hi,
this question is rather tricky to answer in general. A simple approach is to compute the relative error. That works fine as long as there are no zeros in it, i.e.,
absError = abs(A-B);
relError = max(absError(:) ./ abs(A(:)));
This also assumes, that A and B are at least comparable in size, so it does not make a difference if you devide by A or B.
Taking care of all eventualities (NaN, inf, zeros ...) makes it more difficult to answer.
Titus
Anastasios
el 26 de Jun. de 2014
I assume you have some floating point variables, in order to have this 5% error. Try to define a function to check if they are relatively equal, such as
function test = isequalRel(x,y,tol);
test = ( abs(x-y) <= ( tol*max(abs(x),abs(y)) + eps) );
if (! test)
printf("fail x=%e y=%e tol=%e\n", x, y, tol);
end
So basically you do the following :
|a-b| < tol*max(|a|,|b|) + tol_floor
3 comentarios
Anastasios
el 26 de Jun. de 2014
Use the following command
abs(A-B) <= ( tol*max(abs(A),abs(B)) + eps)
where the tol = 0.005 (5%) You will get a logical answer if they are within this error of 5% the answer would be 1 otherwise 0.
Hope it helps
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!