How to add an interval to a number?

3 次查看(过去 30 天)
I have two points (0,0) and (1,1). and I write them as a vector like:
x=[0 1]
y=[0 1]
Also, I take the user two points (xp,yp).
(xp,xp)=ginput;
Now I want to use if structure to do some plots if the points of user and mine are almost the same. My question is I want to add some interval to x,(for being almost equal not exactly). for example:
if strcmp(x+[a, b],xp) && strcmp(y+[a, b]),yp)
but this bring me many errors. Maybe I can't use strcmp in this way or intervals...I don't know!
Could you please help me how can I add some error, interval .. to this? Thank you

采纳的回答

Matt Macaulay
Matt Macaulay 2018-4-18

Using a tolerance as KSSV mentioned does the job.

x=[0 1];
y=[0 1];
[xp,yp]=ginput;
epsilon = .1;
dx = x - xp;
dy = y - yp;
if any(hypot(dx, dy) < epsilon)
    disp('Close')
else
    disp('Far')
end
  2 个评论
RSHU FA
RSHU FA 2018-4-18
Thanks so much. The only problem here I want to have a difference between (0,0),(1,1) and (1,1),(0,0). The order is important. How can I put it in the program?
Matt Macaulay
Matt Macaulay 2018-4-19
The elements of the array
hypot(dx, dy) < epsilon
hold this information

请先登录,再进行评论。

更多回答(2 个)

Pawel Jastrzebski
Pawel Jastrzebski 2018-4-18

This example should have the answers to your problems:

% Values that can be changed in the code:
% d, Xu, Yu
% original points
X = [0 1];
Y = [0 1];
% error as an absolute value
d = 0.1;
% data for P(3) and P(4) below
% indexing vectors for X
idx1  = false(1,8);
idx1(1,[1,4,5,end]) = true; 
idx2  = ~idx1;
% error area X values
Xd = repelem(X,4);
Xd(idx1) = Xd(idx1)-d;
Xd(idx2) = Xd(idx2)+d;
% indexing vectors for Y
idx3 = false(1,8);
idx3(1,[1,2,5,6]) = true;
idx4 = ~idx3;
% error area X values
Yd = repelem(Y,4);
Yd(idx3) = Yd(idx3)+d;
Yd(idx4) = Yd(idx4)-d;
% user defined vector
Xu = [0.05 1.09];
Yu = [0.1 1.05];
% deciding whether baseline vector and user defined vector
% are within error 'd' from each other
Xb = Xd([1:2;5:6]);
Yb = Yd([3,2;7,6]);
Xcompare = (Xb(:,1) <= Xu') & (Xb(:,2) >= Xu');
Ycompare = (Yb(:,1) <= Yu') & (Yb(:,2) >= Xu');
result = all([Xcompare; Ycompare]);
f(1) = figure;
p(1) = plot(X,Y,'ok--');
hold on
p(2) = fill(Xd(1:4),Yd(1:4),[0 1 0]);
p(3) = fill(Xd(5:end),Yd(5:end),[0 1 0]);
p(4) = plot(Xu,Yu,'rx--');
grid on
if result
    title('In');
else
    title('Out');
end
legend([p(1), p(2), p(4)],...
    {'baseline', 'error area', 'user input'},...
    'Location','best');
set([p(2) p(3)],...
    'facealpha', 0.2,...
    'LineStyle', 'none');

The output:


KSSV
KSSV 2018-4-18

Read about ismembertol.

Or you can add some tolerance (may be 10^-3) to (x,y) and then check with (xp,yp).

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by