Azzera filtri
Azzera filtri

"find" function - precision problem?

16 visualizzazioni (ultimi 30 giorni)
Jon Ericson
Jon Ericson il 5 Ott 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

Risposte (2)

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

Accedi per commentare.


Walter Roberson
Walter Roberson il 5 Ott 2011

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by