Find indice of floating point number in matrix
12 views (last 30 days)
Show older comments
Find method allows us to return the indice of a particular element in a matrix/array.
However when the matrix contains floating point values,find generally returns NUll array.
I tried using the following algorithm by adding tolerance but even that fails.
So now 2 questions why does find fail on floating point arrays and secondly how should I go about solving the problem
idx = find(abs(x-2.5)<1e-3);
X is enclosed as follows
0 Comments
Accepted Answer
Jan
on 6 Sep 2021
find() does not fail on floating point arrays. If a floating point array is provided, find replies the indices of all elements, which are not 0.
In your case you provide a logical array: abs(x - 2.5) < 1e-3
Then find() replies the indices of all elements, which have a distance of less than 1e-3 from 2.5. If the output is empty, there is no such element. This is not a failure, but the expected behavior.
The value with the smallest distance to 2.5 in your data set is 0.057631. The distance is larger than 1e-3, so your code works correctly.
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!