# How to add tolerance to my code ?

79 views (last 30 days)
Faez Alkadi on 22 Sep 2017
Commented: Faez Alkadi on 26 Sep 2017
I have this code to flipud every other line along x-axis. The code works (only) when the increment on x-axis is an integer number(1,2,3...or 4 etc) as in data (a) attached, result shown in the first figure for 2 increment . but when the increment on x-axis is a fraction(1.123 or 1.234, ...or etc) as in data (b) attached, it only work for the first line as shown in the second Figure for 1.123 increment.
I think the reason is that I need to add a tolerance to the result of desired_line so Matlab can find it and add it to the desired_line_matrix. because of precision issue.
_ But i don't know how to add the tolerance of search !!!!_
Thank you so much
%Find the desired lime:
desired_line_matrix=[];
TS=2;
s=a;
desired_line=[];
line_number=(max(s(:,1))-min(s(:,1)))/TS;
for j=0:TS:line_number*2
desired_line =s(1,1)+(j+min(s(:,1)));
desired_line_matrix=[desired_line_matrix,desired_line];
end
%Flip the desired lime:
for i=1:length(desired_line_matrix)
[r,c]=find(s(:,1)==desired_line_matrix(i));
d=r';
s(d,:)=flipud(a(d,:));
end
plot(s(:,1),s(:,2))
xlabel('X')
ylabel('Y')
hold on

Image Analyst on 22 Sep 2017
use ismembertol(), or else
tolerance = 0.001; % whatever....
closeEnough = abs(s(:,1) - desired_line_matrix(i)) <= tolerance;
[r,c]=find(closeEnough);
Faez Alkadi on 26 Sep 2017
Hi,
First of all I would like to thank you for your effort.
What I meant by "it only work for the first line" is that it only flip upside down the first line where I wanted it to flip every other line to form square wave regardless of what TS is.
But I think using your first answer is going to work for me if i can set the right tolerance.
tolerance = 0.001; % whatever....
closeEnough = abs(s(:,1) - desired_line_matrix(i)) <= tolerance;
[r,c]=find(closeEnough);
I will work on it and see how it goes !!!
Thank you again.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by