find関数について
6 views (last 30 days)
Show older comments
pix = 0.001;
a = -1:pix:1;
find(a==0.1)
配列aには0.1が格納されているにもかかわらず,findでインデックスを得ることができません.
ほかに要素のインデックスを得る方法がありましたら教えていただけると幸いです.
0 Comments
Answers (1)
Dyuman Joshi
on 16 Feb 2024
Edited: Dyuman Joshi
on 16 Feb 2024
Welcome to the world of floating point numbers, where not all numbers can be represented exactly in binary form.
See this thread for more information - https://in.mathworks.com/matlabcentral/answers/57444-why-is-0-3-0-2-0-1-not-equal-to-zero?s_tid=faqs_link
When comparing floating point numbers, the best practice is to use a tolerance -
pix = 0.001;
a = -1:pix:1;
tol = 1e-6;
idx = find(abs(a - pix) < tol)
%check
a(idx)
k = ismembertol(a, pix);
IDX = find(k)
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!