Comparing two matrix in Matlab

1 回表示 (過去 30 日間)
john vattic
john vattic 2014 年 6 月 26 日
コメント済み: Anuj Kumar 2020 年 12 月 24 日
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
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
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
  1 件のコメント
john vattic
john vattic 2014 年 6 月 26 日
i got inf as result of relError

サインインしてコメントする。


Anastasios
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
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
vanita
vanita 2014 年 8 月 6 日
how to decide tolerance ? is their is any standard?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by