Why does "find" command fail to find a number in this vector?

조회 수: 4 (최근 30일)
Emmanuele Mereu
Emmanuele Mereu 2017년 4월 12일
편집: Stephen23 2017년 4월 12일
Hello everybody, I use MATLAB as a beginner and I would like to understand why the "find" command does not work in this particular case.
I must know the position occupied by the number 0.24 in this vector:
v=0.01:0.01:4;
In the variable editor I see that 0.2400 is stored in row 1, column 24, so I wrote this instruction:
pointer=find(v==0.24)
in the workspace without ";" so that i can see the output, but instead of "pointer=24" as I expected this message appeared:
"pointer = 1×0 empty double row vector"
How is that possible?
Also, if I search other numbers it happens that "find" can determine the position of, for example, 0.25 but it returns the same message of empty vector searching 0.29.
Thaks.
  댓글 수: 2
Emmanuele Mereu
Emmanuele Mereu 2017년 4월 12일
편집: Stephen23 2017년 4월 12일
Using fprintf('%.30f\n',v(24)) as you suggested in the first link I realized the problem. Tank you again for the quick answer.

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

채택된 답변

Jan
Jan 2017년 4월 12일

추가 답변 (2개)

Santhana Raj
Santhana Raj 2017년 4월 12일
The issue is with representation of floating point numbers. Since the variable 'v' is double, it cannot exactly represent/store 0.024. Hence you cannot compare for exact value. Instead you can try this:
find((v<=0.245) & (v>=0.239))
Hope this helps.

Thorsten
Thorsten 2017년 4월 12일
You can use
find(round(100*v) == 24)
The reason that the straight-forward approach does not work is the representation of floating point numbers, as already mentioned above.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by