Indexing nearest value to my coordinate
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone, I have a list "Stations" (59x2) with coordinates (lon, lat).
I also have a .nc file that contains longitude & latitude (both 621x837) and a describing variable "v".
I now want to find the indices of the longitude and latitude in the .nc file, that are closest/nearest to each coordinate in my list "Stations" because they do not always match exactly.
The nc file is unfortunately way too large to attach to this question.
My code looks like this:
%% Data import and information
filename = 'D:\xxx\ccc\vvv\1999.nc';
%% Step 1 - Variables: Load latitude and longitude arrays
lon_data = ncread(filename, 'lon');
lat_data = ncread(filename, 'lat');
t_data = ncread(filename, 'time');
%% Step 2: Define the coordinates of the 59 points of interest
Stations = [14.0355, 61.2536;
... % 58 other coordinates that I can add if needed (otherwise it makes the code confusing)
];
%% Step 3: Find the indices of the points of interest
lat_indices = zeros(59, 1);
lon_indices = zeros(59, 1);
eps = 0.0005;
for i = 1:59
[~, lat_idx] = abs(lat_data - Stations(i, 2));
[~, lon_idx] = abs(lon_data - Stations(i, 1));
lat_indices(i) = find((lat_idx < eps), 1);
lon_indices(i) = find((lon_idx < eps), 1);
end
The code, however, returns the following error:
Error using abs
Too many output arguments.
Error in Indexing_lat_lon (line 83)
[~, lat_idx] = abs(lat_data - Stations(i, 2));
0 Commenti
Risposta accettata
Torsten
il 31 Lug 2023
Modificato: Torsten
il 31 Lug 2023
lon_data = rand(621,837);
lat_data = rand(621,837);
stations = rand(59,2);
lon_indices = zeros(59, 1);
lat_indices = zeros(59, 1);
for i = 1:59
[~,lon_indices(i)] = min(abs(lon_data-stations(i,1)),[],"all","linear");
[~,lat_indices(i)] = min(abs(lat_data-stations(i,2)),[],"all","linear");
end
[row_lon,col_lon] = ind2sub([621 837],lon_indices)
[row_lat,col_lat] = ind2sub([621 837],lat_indices)
It's strange that you want the min for lon and lat separately. Shouldn't you match one index pair (ilon,ilat) common for both nc-files ?
Shouldn't it be
for i = 1:59
[~,lonlat_indices(i)] = min((lon_data-stations(i,1)).^2+(lat_data-stations(i,2)),[],"all","linear");
end
[row_lonlat,col_lonlat] = ind2sub([621 837],lonlat_indices)
0 Commenti
Più risposte (1)
Bruno Luong
il 31 Lug 2023
Modificato: Bruno Luong
il 31 Lug 2023
It is odd that you want to find the index of nc file data closest to the Station.
It just sounds more natural to look for the opposite, index of the Station closest to the nc data.
Note that the distance here is euclidian on (lon,lat) which is NOT the geodesic distance.
lon_data = rand(10,10);
lat_data = rand(10,10);
Stations = rand(59,2);
DT = delaunayTriangulation(lon_data(:), lat_data(:));
idx = DT.nearestNeighbor(Stations)
% alternatively if you have the Statistics and Machine Learning Toolbox
%idx = knnsearch([lon_data(:), lat_data(:)], Stations)
figure
h1 = plot(lon_data, lat_data, '+g');
hold on
h2 = plot(Stations(:,1),Stations(:,2), 'or');
Lon = [Stations(:,1),lon_data(idx)]'; Lon(3,:) = NaN;
Lat = [Stations(:,2),lat_data(idx)]'; Lat(3,:) = NaN;
h3 = plot(Lon, Lat, 'b');
legend([h1(1) h2(1) h3(1)], 'nc data', 'stations', 'nearest nc')
axis equal
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!