Why matrix return all 'false/0' when there are a lot of similar values between matrix and a column vector?
1 view (last 30 days)
Show older comments
Hi all,
I have a text file which I imported in MATLAB and now it's in the form of table. It has latitude longitude values which I want to compare with other lat lon values. Data from text file is attached.
Secondly, there is a .hdf file containing lat lon values in the form of matrix. I used interpolation because size of column vector (to be matched) in text file is 7570x1. Initial size of lat lon from hdf image is:
%initial size
lat_hdf = 406x270
lon_hdf = 406x270
%interpolation
lat1 = imresize(lat_hdf,[7570 270],'bilinear');
lon1 = imresize(lon_hdf,[7570 270],'bilinear');
The problem is when I want to match the text file lat lon with lat1 and lon1 it returns a matrix with all 0. Means all false.
However there are alot of values that are similar in these two. I don't know what's the issue.
2 Comments
Accepted Answer
KSSV
on 6 Sep 2021
You should not compare the floting point numbers like that. You should check the difference between them to be less than some tolerance.
tol = 10^-3 ;
Mat = (lat-lat_txt)<=tol;
0 Comments
More Answers (1)
Walter Roberson
on 6 Sep 2021
Mat = (lat1==lat_txt);
The == operator is only bit-for-bit identical values (except that -0 is considered to be equal to 0). But you did
lat1 = imresize(lat_hdf,[7570 270],'bilinear');
so the values in lat1 are computed values -- and they might only be "close", or they might have slightly different round-off in the last bit or two.
Never use == to compare for equality with computed floating point values, except for cases where the value being compared is extracted from the other value. For example it is valid to do
a = rand(1,10);
maxa = max(a);
a == maxa
because maxa will be a bit-for-bit equivalent to some value in a.
It is common to see people do things like
b = 0:.1:1;
b*10 == 3
and be surprised that it is not true for any positions in b. 0.1 is not exactly representable in double precision, and the : operator does not interpret the operation as requesting b = (0:10)/10
Anyhow, you should use ismembertol() instead of ==
See Also
Categories
Find more on HDF5 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!