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

조회 수: 3 (최근 30일)
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
Stephen23
Stephen23 2021년 1월 25일
편집: Stephen23 2021년 1월 25일
"...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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2021년 1월 25일
편집: Stephen23 2021년 1월 25일
"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
Stephen23
Stephen23 2021년 1월 27일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by