How to add an interval to a number?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (2)
Pawel Jastrzebski
le 18 Avr 2018
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 commentaire
RSHU FA
le 18 Avr 2018
KSSV
le 18 Avr 2018
0 votes
Read about ismembertol.
Or you can add some tolerance (may be 10^-3) to (x,y) and then check with (xp,yp).
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!