How to add tolerance to my code ?

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

 Accepted Answer

use ismembertol(), or else
tolerance = 0.001; % whatever....
closeEnough = abs(s(:,1) - desired_line_matrix(i)) <= tolerance;
[r,c]=find(closeEnough);

9 Comments

i have made some changes in the code(just some names), and I believe that the first for loop is the one responsible about finding the desired line. so i need to add a tolerance to the desired_line and then add it to the desired_line_matrix !!!
HOPE YOU TAKE A LOOK AGAIN !
THANKS
desired_line_matrix=[];
s=b;
TS=2;
desired_line=[];
line_number=(max(s(:,1))-min(s(:,1)))/TS;
for j=0:TS*2:line_number*TS
desired_line =s(1,1)+(j+min(s(:,1)));
desired_line_matrix=[desired_line_matrix,desired_line];
end
for i=1:length(desired_line_matrix)
[r,c]=find(s(:,1)==desired_line_matrix(i));
d=r';
s(d,:)=flipud(s(d,:));
end
plot(s(:,1),s(:,2))
xlabel('X')
ylabel('Y')
hold on
What is b (can't run the code without it)? And any other values I'd need to run the code. In the meantime, see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
I just updated everything in the code and added both a and b.
Thanks
desired_line_matrix=[];
s=b;
TS=1.123;%chang this to 2 for a
desired_line=[];
line_number=(max(s(:,1))-min(s(:,1)))/TS;
for j=0:TS*2:line_number*TS
desired_line =s(1,1)+(j+min(s(:,1)));
desired_line_matrix=[desired_line_matrix,desired_line];
end
for i=1:length(desired_line_matrix)
[r,c]=find(s(:,1)==desired_line_matrix(i));
d=r';
s(d,:)=flipud(s(d,:));
end
plot(s(:,1),s(:,2))
xlabel('X')
ylabel('Y')
hold on
Where is the line where you set a and b? Because I'm not seeing it and copying and pasting the code just throws the same error. Fix it and I'll check again sometime tomorrow.
The first two lines must changed according to the increment.
If you put s=a, then TS=2. But if you put s=b, then TS=1.123.
New a and b are attached here.
And the code is working fine just with copy and past. they just need to change both s and TS together.
Thank you again. I appreciate your help.
s=b;% here is the line where you set a and b
TS=1.123; % Chang this to 2 for a
desired_line=[];
desired_line_matrix=[];
line_number=(max(s(:,1))-min(s(:,1)))/TS;
for j=0:TS*2:line_number*TS
desired_line =s(1,1)+(j+min(s(:,1)));
desired_line_matrix=[desired_line_matrix,desired_line];
end
for i=1:length(desired_line_matrix)
[r,c]=find(s(:,1)==desired_line_matrix(i));
d=r';
s(d,:)=flipud(s(d,:));
end
plot(s(:,1),s(:,2))
xlabel('X')
ylabel('Y')
hold on
You're missing the lines of code where you call load('a.mat') etc. Can you include those?
Faez Alkadi
Faez Alkadi on 25 Sep 2017
Edited: Faez Alkadi on 25 Sep 2017
Hi,
I really don't know what you mean. Because according to my knowledge is taht you just need to download a and b to your PC. and double click on them and they will be loaded to the work space on Matlab. and then you just change s and TS in the code accordingly.
Thank you again.
Never mind. I added the code to the beginning of the file myself. It's below to help anyone else who might want to try to help you:
% Read in a.mat file, using the load() function, into a structure.
storedStructure = load('a.mat')
% Take the "s" array of the structure and copy it into a.
a = storedStructure.s;
% Read in b.mat file, using the load() function, into a structure.
storedStructure = load('b.mat')
% Take the "s" array of the structure and copy it into b.
b = storedStructure.s;
s=b;% here is the line where you set a and b NO IT'S NOT. You use b here, not SET it.
TS=1.123; % Change this to 2 for a
desired_line=[];
desired_line_matrix=[];
line_number=(max(s(:,1))-min(s(:,1)))/TS;
for j=0:TS*2:line_number*TS
desired_line =s(1,1)+(j+min(s(:,1)));
desired_line_matrix=[desired_line_matrix,desired_line];
end
for i=1:length(desired_line_matrix)
[r,c]=find(s(:,1)==desired_line_matrix(i));
d=r';
s(d,:)=flipud(s(d,:));
end
plot(s(:,1),s(:,2))
xlabel('X')
ylabel('Y')
hold on
But I'm not really sure what you want. It produces the plot you showed, but what is wrong with that? What do you mean by "it only work for the first line"? What is being plotted? One line? All lines? What would you expect it to look like? A square wave regardless of what Ts is?
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!