"find" function - precision problem?
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Sean de Wolski
am 5 Okt. 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
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!