How can the "find" function fail at finding elements in an array?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ion CANDEL
am 29 Okt. 2016
Bearbeitet: Star Strider
am 29 Okt. 2016
Dear all,
I have used many times the "find" function with very good results... until now. Here is my VERY SIMPLE code:
a = [0.145:0.0005:0.175];
index = find(a == 0.16)
index =
Empty matrix: 1-by-0
index2 = find(a == 0.15)
index2 =
11
So... both numbers (0.15 and 0.16) EXIST in the array named "a", however, ONLY 0.15 IS FOUND...
I have checked this in R2015a and R2013b with the same results... Does anybody haves an idea of what is going on???
Best regards, Ion
0 Kommentare
Akzeptierte Antwort
Star Strider
am 29 Okt. 2016
Bearbeitet: Star Strider
am 29 Okt. 2016
You have discovered ‘floating point approximation error’.
EDIT —
To illustrate:
a = [0.145:0.0005:0.175];
index = find(a <= 0.16, 1, 'last')
result = a(index)
err = result - 0.16 % Floating Point Error
result =
0.16
err =
-2.7756e-17
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!