Сравнение двух матриц в Matlab

Во многих случаях

display:inline;

достаточно.

Но в некоторых случаях вы должны добавить следующее:

clear:none;
14
задан MatlabDoug 2 June 2009 в 14:55
поделиться

3 ответа

Modifying Edric's solution:

absTol = 1e-3;   % You choose this value to be what you want!
relTol = 0.05;   % This one too!
absError = x(:)-y(:);
relError = absError./x(:);
relError(~isfinite(relError)) = 0;   % Sets Inf and NaN to 0
same = all( (abs(absError) < absTol) & (abs(relError) < relTol) );

The variable same will be false if either the absolute or the relative error of any element is larger than whatever tolerances you choose. Also, if any elements of x happen to be exactly 0, then some of the elements of relError could end up being either infinite or not-a-number, so I used the ISFINITE function to ignore those values by setting them to 0.

I wouldn't suggest using IMAGESC to compare plots, since 1) the data is scaled when it is displayed, 2) the colormap for the display has a discrete number of color values (which I think is 256 by default, hence lots of rounding), and 3) subtle variations in color may not be all that apparent from visual comparison of two plots.

5
ответ дан 1 December 2019 в 12:53
поделиться

Попробуйте следующее:

tf = abs((A-B)./B)<0.05

Это вернет логическую матрицу, которая будет истинной для каждого элемента, если относительная разница между A и B относительно B составляет менее 5 процентов.

Если вы хотите спросить, все ли это правда (все они удовлетворяют вышеуказанному условию):

all(tf(:))
13
ответ дан 1 December 2019 в 12:53
поделиться

Я бы подумал о том, чтобы сделать что-то вроде этого с абсолютным допуском, а также с относительным допуском:

function same = tol( x, y )
absTol = 1e-3;
relTol = 0.05;
errVec = abs( x(:) - y(:) );
same = all( (errVec < absTol) | (errVec./x(:) < relTol) );
4
ответ дан 1 December 2019 в 12:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: