フィルターのクリア

"find" function - precision problem?

16 ビュー (過去 30 日間)
Jon Ericson
Jon Ericson 2011 年 10 月 5 日
Hi Folks,
I'm trying to match values in two double precision matrices but it's not finding the line numbers where the values obviously match. Is MATLAB hiding some decimal places that get factored into the find function?
Time(1) = 25.9947
T =
25.9117
25.9281
25.9484
25.9666
25.9802
25.9947
26.0116
26.0280
26.0486
26.0617
26.0802
>> find(T==Time(1))
ans =
Empty matrix: 0-by-1
I'm using the following code to build these two matrices in the first place (the underlying numerical data is in .txt files).
fid=fopen('explorationmaster.txt');
Str=textscan(fid,'%s','delimiter',',');
Time=Str(2:2:end);
Time=str2double(Time);
In the original text files, Time(1) looks like this:
25.994734107267824
So it appears MATLAB is chopping it down to 25.9947, which is good because it then matches an entry in T exactly... so why doesn't find pick up on the fact that these two values match?
Thanks! Jon

回答 (2 件)

Sean de Wolski
Sean de Wolski 2011 年 10 月 5 日
no, it's still the full double precision, you're just not seeing it; show the full thing using format long.
format long
Time(1)
To reduce the tolerance use an absolute difference with a tolerance:
find(abs(T-Time(1))<10^-6) %right to 10^-6
  1 件のコメント
Jon Ericson
Jon Ericson 2011 年 10 月 5 日
thanks very much, the tolerance worked very well!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 10 月 5 日

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by