Comparing two matrix in Matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Anuj Kumar
2020년 12월 24일
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
답변 (2개)
Titus Edelhofer
2014년 6월 26일
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
2014년 6월 26일
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
Anastasios
2014년 6월 26일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!