How to add an interval to a number?

조회 수: 3 (최근 30일)
RSHU FA
RSHU FA 2018년 4월 18일
댓글: Matt Macaulay 2018년 4월 19일
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:

  댓글 수: 1
RSHU FA
RSHU FA 2018년 4월 18일
Thanks a lot. It's very good exercise.

댓글을 달려면 로그인하십시오.


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