find is not matching a value of 1 in an array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jeremy Quandt
am 19 Mär. 2020
Kommentiert: Jeremy Quandt
am 19 Mär. 2020
I am unable to match a value of 1 with find when checking an array produced by linspace. Is this as expected behavior?
% Create our k samples.
k = linspace(0.1, 2.0, 20)
% Find indexes.
[find(k==0.5) find(k==1.0) find(k==1.5)]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278214/image.jpeg)
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 19 Mär. 2020
Bearbeitet: Adam Danz
am 19 Mär. 2020
Here's a demo showing the roundoff error.
k = linspace(0.1, 2.0, 20); % 1 is in positon 10
k(10) - 1
ans =
-1.1102e-16
Here are alternatives.
% find the value of 1
idx = find(ismembertol(k,1));
or
% find the value of 1
[~, idx] = min(abs(k-1))
Weitere Antworten (1)
Loren Shure
am 19 Mär. 2020
You are running into an issue of floating point arithmetic. You might use ismembertol : mathworks.com/help/matlab/ref/ismembertol.html
Siehe auch
Kategorien
Mehr zu Signal Analysis 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!