"find" function - precision problem?

18 views (last 30 days)
Jon Ericson
Jon Ericson on 5 Oct 2011
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

Answers (2)

Sean de Wolski
Sean de Wolski on 5 Oct 2011
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 Comment
Jon Ericson
Jon Ericson on 5 Oct 2011
thanks very much, the tolerance worked very well!

Sign in to comment.


Walter Roberson
Walter Roberson on 5 Oct 2011

Categories

Find more on Characters and Strings 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!