problem with the setdiff function

3 Ansichten (letzte 30 Tage)
alex
alex am 17 Mär. 2014
Kommentiert: alex am 17 Mär. 2014
Hello!
i have a problem here
A = 2:.1:15
B=[9.4000 10.4000]
C=setdiff(A,B)
the elements 9.4000 and 10.4000 are still in the C
however,if B=[9.3000 10.3000] then the setdiff works fine,and the elements are not in the C.
any idea why this happens?
  2 Kommentare
per isakson
per isakson am 17 Mär. 2014
Try
format hex
alex
alex am 17 Mär. 2014
unfortunatelly the format hex is not working

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 17 Mär. 2014
Bearbeitet: Azzi Abdelmalek am 17 Mär. 2014
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 17 Mär. 2014
Bearbeitet: Azzi Abdelmalek am 17 Mär. 2014
Use some tolerance
A = 2:.1:15
B=[9.4000 10.4000 1000]
tol=0.0001
s=abs(bsxfun(@minus,A',B))<tol
[ii,jj]=find(s==1);
A(ii)=[]
alex
alex am 17 Mär. 2014
thank you very much!
it works excellent!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jos (10584)
Jos (10584) am 17 Mär. 2014
A = 2:.1:15
B = [9.4000 10.4000]
D = 2 ; % number of digits that should match (e.g. 1.231 = 1.229)
C = setdiff(round(A*10^D),round(B*10^D)) ./ (10^D)
  1 Kommentar
alex
alex am 17 Mär. 2014
thank you for your answer! very useful!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by