# Ho to compare two array with tolerance?

16 views (last 30 days)
Namrata Biranje on 8 May 2018
Commented: Stephen23 on 8 May 2018
I have two 2 dimensional arrays as follows: at time step 0.1 to 1.1
A = [0 0 0 1 1 1 0 0 0 1 1 0 0 0..] B = [0 0 0 0 0 1 1 1 0 0 0 1 1 0..]
One more array Diff which is the differance of A and B Diff = [0 0 0 1 1 0 -1 -1 0 1 1 -1 -1 0...]
my logic is
lwrlmt=-0.3 upperlmt = 0.3
for j=1:length(Diff)
if (Diff(j,2)~= 0)
if (Diff(j-1,2)==0) && (Diff(j+1,2)~=0)
Tm1 = Diff(j,1); % if here TM1 = 0.4
end
if (Diff(j-1,2)~=0)&& (Diff(j+1,2)==0)
Tm2 = Diff(j,1); % Tm2= 0.5
Rais_diff=(Tm2-Tm1)*1000; %0.5-0.4=0.1
R_temp=int2str(Rais_diff);
Tolerance = str2double(R_temp);
if ((lwrlmt<=Tolerance) && (Tolerance<=upperlmt))
disp('A and B are equal');
else
disp('A and B are not equal');
break;
end
end
end
But above logic is failing when Diff array is
[ 0 0 0 0 0 0 1 1 1 1 1 1 ..] or [ 1 1 1 1 1 1 1 0 0 0 0]
how should I proceed further?
##### 2 CommentsShowHide 1 older comment
Stephen23 on 8 May 2018
@Namrata Biranje: please explain what you are trying to achieve, and explain the rules of what you want the code to do. Showing us broken code is not as helpful as you actually telling us in words what you want. Please also show us complete examples with inputs and outputs, so that we have something to run code on and compare against.