Matlab round the values and find function can not give the exact result(index)?!

8 views (last 30 days)
I have a 2D array of size 10767*10136 from lat/long which means the values are unique. One pixel value of this array is -69.723897531281570 if I look for -69.723897 the result would more than one value. I used this expression format longE to consider the actual values, the outcome would be the same as before.
Also, I tried some inventory techniques to multiply 2D array and x value by 1000000 to eliminate the incapability of Matlab in processing a large array with high accuracy, but not reliable results?! Is there a solution to this problem?!
format longE
field2 = ncread(filenames,'lat') ; % to read 2D array data
x = -69.723897; % value that I am searching for
round_field2 = round(field2,6 ); % I am rounding to find the exact value and eliminationg the long deciaml value -69.723897531281570
use_minus_method = round_field2 - x; % to find the 0 value that sould be one just one 0
[ idx , idm] = find(use_minus_method == 0 );
% % % the other technique
x = -69.723897; % value that I am searching for
round_field2 = round(field2,6 ); % I am rounding to find the exact value and eliminationg the long deciaml value -69.723897531281570
format longE % also I tried in this place
[ idx , idm] = find(round_field2 == x ); % directly finding x
% % % the other technique
x = -69.723897; % value that I am searching for
x = string(x); % create string
round_field2 = round(field2,6 ); % I am rounding to find the exact value and eliminationg the long deciaml value -69.723897531281570
round_field_string = string(round_field2); % create string
[ idx , idm] = find(contains(round_field_string,x)); %find x using the contain method
  1 Comment
Stephen23
Stephen23 on 25 Jan 2021
Edited: Stephen23 on 25 Jan 2021
"...to eliminate the incapability of Matlab in processing a large array with high accuracy..."
MATLAB uses exactly the same binary floating point numbers that almost every other numeric application uses, because they are supported by all modern computer HW and important linear algebra libraries, enabling fast and efficient numeric operations. Their accuracy is around 15-17 decimal digits, which is on par with the most precisely measured quantities in all of physics, e.g.:
Apparently this precision is inadequate for your data, for which I am deeply impressed, as this means that your experimental data are more accurately measured than every other measurement in all of human history. How did you achieve this remarkable feat?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2021
Edited: Stephen23 on 25 Jan 2021
"I used this expression format longE to consider the actual values"
format longE does not show the "actual values", it just shows the values with fifteen digits after the decimal place (for doubles). If you really want to see the "actual values" then download this:
Neither rounding nor converting to string will resolve the cause of your difficulties. The actual solution is to not to measure for exact equivalence, but instead compare the absolute difference against a tolerance:
abs(A-B)<tol
For example:
V = [-69.72389752,-69.723897531281570,-69.72389754];
x = -69.72389753;
tol = 5e-9;
idx = abs(V-x)<tol
idx = 1x3 logical array
0 1 0
Read more about binary floating point numbers:
This is worth reading as well:
  7 Comments
Stephen23
Stephen23 on 27 Jan 2021
Most likely you will have to experiment with the required tolerance. To start it might help to get a sorted list of the differences using that data set, from which you can decide what it means for those values to be considered different (or not).

Sign in to comment.

More Answers (0)

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!