function find() sometimes doesn't work properly

조회 수: 4 (최근 30일)
Pavel M
Pavel M 2024년 7월 18일
편집: Stephen23 2024년 7월 20일
hello! i have a simple part of code, but in some cases function find doesnt work
f = [10 : 0.001 : 60];
frez = [48.7234 48.4347 46.4930 46.7682 44.9716 45.9232 48.2044 47.7394 55.0087 49.9675];
for i = 1 :length(frez)
x1 = find(f == round(frez(i)-5, 3));
x2 = find(f == round(frez(i)+5, 3));
Sd{i} = {x1 x2};
end
Sd{3}, Sd{8}, Sd{9} have 1 empty value! Why?
round(frez(3)-5, 3)
ans =
41.4930
>> find(f==ans+0.001)
ans =
31495
>> f(31494)
ans =
41.4930
But that code work!
Why 'find' doesnt find index even though it is?
  댓글 수: 1
Stephen23
Stephen23 2024년 7월 18일
편집: Stephen23 2024년 7월 20일
"function find() sometimes doesn't work properly"
What is more likely is some binary floating point numbers have different values.
"Why 'find' doesnt find index even though it is?"
Because it isn't:
f = 10:0.001:60; % got rid of the superfluous square brackets
frez = [48.7234 48.4347 46.4930 46.7682 44.9716 45.9232 48.2044 47.7394 55.0087 49.9675];
for i = 1 :length(frez)
x1 = find(f == round(frez(i)-5, 3));
x2 = find(f == round(frez(i)+5, 3));
Sd{i} = {x1 x2};
end
format hex
round(frez(3)-5, 3)
ans =
4044bf1a9fbe76c9
f(31494)
ans =
4044bf1a9fbe76c8
The same value? Nope, different values.
So far everything seems to be working exactly as documented and expected.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2024년 7월 18일
With Floating-Point Numbers you need to use a tolerance, so with find, usually one of the approaches in tthe second loop will work —
f = [10 : 0.001 : 60];
frez = [48.7234 48.4347 46.4930 46.7682 44.9716 45.9232 48.2044 47.7394 55.0087 49.9675];
for i = 1 :length(frez)
x1 = find(f == round(frez(i)-5, 3));
x2 = find(f == round(frez(i)+5, 3));
Sd{i,:} = {x1 x2};
end
for k = 1:numel(Sd)
Sdvec = Sd{k}
end
Sdvec = 1x2 cell array
{[33724]} {[43724]}
Sdvec = 1x2 cell array
{[33436]} {[43436]}
Sdvec = 1x2 cell array
{1x0 double} {[41494]}
Sdvec = 1x2 cell array
{[31769]} {[41769]}
Sdvec = 1x2 cell array
{[29973]} {[39973]}
Sdvec = 1x2 cell array
{[30924]} {[40924]}
Sdvec = 1x2 cell array
{[33205]} {[43205]}
Sdvec = 1x2 cell array
{1x0 double} {[42740]}
Sdvec = 1x2 cell array
{[40010]} {1x0 double}
Sdvec = 1x2 cell array
{[34969]} {[44969]}
f = [10 : 0.001 : 60];
frez = [48.7234 48.4347 46.4930 46.7682 44.9716 45.9232 48.2044 47.7394 55.0087 49.9675];
for i = 1 :length(frez)
x1 = find(f >= round(frez(i)-5, 3), 1, 'first');
x2 = find(f <= round(frez(i)+5, 3), 1, 'last');
Sd{i,:} = [x1 x2];
end
Out = cell2mat(Sd)
Out = 10x2
33724 43724 33436 43436 31495 41494 31769 41769 29973 39973 30924 40924 33205 43205 32740 42740 40010 50001 34969 44969
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

추가 답변 (1개)

DGM
DGM 2024년 7월 18일
이동: Steven Lord 2024년 7월 18일
Floating point arithmetic has limited precision
f = [10 : 0.001 : 60];
frez = [48.7234 48.4347 46.4930 46.7682 44.9716 45.9232 48.2044 47.7394 55.0087 49.9675];
format long
round(frez(3)-5, 3)
ans =
41.493000000000002
f(31494)
ans =
41.492999999999995
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 7월 18일
IEEE Double Precision arithmetic is not able to exactly represent 0.001, as it operates in binary instead of in decimal. The reasons are the same as the reason why finite decimal is not able to exactly represent 1/3. No matter what base you use for finite calculations, there are going to be finite values that cannot be exactly represented.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by