'Find' command to find index of lat and long from an array

I am trying to extract the index of latitude and longitude from an array using 'find' command. My lat/long array contains latitude and longitude for the whole globe. I am using a code where I require only couple of lat/longs to be matched from the global array but its showing me that there is no match. Below is the code if anybody can help:
LG=[52.25 53.25 54.25]; % desired longitude values
LT=[21.65 22.65 23.65]; % desired latitude values
% Step 2 : Display all varaibles in nc file using command: ncdisplay
filename='3B-DAY.MS.MRG.3IMERG.20180203-S000000-E235959.V06.nc4'
ncdisp(filename,'/','min')
% Step 3 : Read varaibles from nc file by command : ncread
%Variable=input('write your main variable=');
precip=ncread(filename,'precipitationCal');
long=ncread(filename,'lon');
lat=ncread(filename,'lat');
time=ncread(filename,'time');
% Step 4 : Find index of longitude and latitude in the dimension of array
% using command : find
for i=1:3
LGG=find(long == LG(i));
LTT=find(lat == LT(i));
if isempty(LGG)||isempty(LTT)
disp('NaN')
break
end
Here I get 'NAN' display. Can somebody tell where is the error? I have attached the nc file as well.

 Respuesta aceptada

KSSV
KSSV el 30 de Mayo de 2022
You should not use find. As lat, lon are floating point numbers using == will not work. You need to define a small tolerance value and check the difference.
tol = 10^-3 ;
for i=1:3
LGG=abs(long-LG(i))<tol;
LTT=abs(lat-LT(i))<tol;
if LGG || LTT % check this logic accordingly
disp('NaN')
break
end
end
You need not to use a loop. You can do this in one line.
Also have a look on knnsearch

5 comentarios

Thanks for the answer. I checked this, first the LGG and LTT difference arrays are becoming all zeros. And its giving me following error:
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.
Error in Extract_Imerge_Coordinates (line 23)
if LGG || LTT % check this logic accordingly
Try
LGG | LTT
You want the difference to be < tol, so your NaN test should be
LGG=abs(long-LG(i))<tol;
LTT=abs(lat-LT(i))<tol;
LLGp = find(LGG);
LTTp = find(LTT);
if isempty(LGGp) | isempty(LTTp)
disp('NaN')
else
precip(LLGp, LTTp) %might have to be precip(LTTp, LLGp)
end
KSSV
KSSV el 30 de Mayo de 2022
Ohh yes...@Walter Roberson. I missed the output is an logical array.
Thanx alot, It solved my issue.

Iniciar sesión para comentar.

Más respuestas (1)

Your code assumes that exactly those latitudes and longitudes appear in the file. You are testing for bit-for-bit identical representations of numbers that binary floating point inherently cannot store the exact representation of. Binary floating point cannot represent 1/10 or 1/100 exactly so for example,
fprintf('%.999g\n', 21.65)
That would have to be the exact value stored in the file, not just something that rounded to the 21.65 to the nearest 1/100.
I suggest that you use interp2() instead of looking for exact equality.

1 comentario

Yes, I understand that point. I've also given the exact matched values as my desired numbers after looking in the array for entire globe, still its not working.

Iniciar sesión para comentar.

Preguntada:

el 30 de Mayo de 2022

Comentada:

el 30 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by