Azzera filtri
Azzera filtri

Filter rows and columns (find)

8 visualizzazioni (ultimi 30 giorni)
I load ('Lat_and_Lon.mat') load('Lat_and_Lon.mat') is in the following google drive link because it is larger than 5 MB (https://drive.google.com/drive/folders/18GgPs-iN3brZHQ3pZNK8GyLt4SOmemY8?usp=drive_link)
The following script is performing processing to filter rows and columns and then cross-reference them.
clc; clear
load('Lat_and_Lon.mat')
%lat_h12v09 = lat_h12v09';
%lon_h12v09 = lon_h12v09';
%load('dat_era5_201001.mat', 'lat');
%lat = lat_h12v09;
%load('dat_era5_201001.mat', 'lon');
%lon = lon_h12v09;
% lat_idx = find(lat >= & lat <=); % índices das latitudes dentro do intervalo desejado
% lon_idx = find(lon >= & lon <= ); % índices das longitudes dentro do intervalo desejado
intervaloLat = [-3, -2];%[-2.9580, -2.8231];
intervaloLon = [-55,-54];%[-60.0372,-59.9023];
latDentroDoIntervalo = lat_h12v09 >= intervaloLat(1) & lat_h12v09 <= intervaloLat(2);
lonDentroDoIntervalo = lon_h12v09 >= intervaloLon(1) & lon_h12v09 <= intervaloLon(2);
%latDentroDoIntervalo = latDentroDoIntervalo'; lonDentroDoIntervalo = lonDentroDoIntervalo';
LatIndex=find(all(latDentroDoIntervalo,1));
%%
LonIndex = [];
for lin = 1:size(lonDentroDoIntervalo, 2)
indices = find(lonDentroDoIntervalo(lin,:) == 1);%(:, col) == 1);
if ~isempty(indices)
LonIndex = [lin LonIndex];
end
end
%%
lat_resultado = lat_h12v09(LonIndex,LatIndex);%lat_h12v09(RowIndex,ColIndex);
lon_resultado = lon_h12v09(LonIndex,LatIndex');%lon_h12v09(RowIndex,ColIndex);
Note that I have lon_result and lat_result. When I conduct my test to check if my bounding filter is correct according to the square plot plus the values of lon_result and lat_result, lon_result comes out larger than it should be, unlike lat_result which is correct.
%test to check if lon_resultado and lat_resultado are within the delimitation intervaloLat = [-3, -2]; intervaloLon = [-55,-54]
% Coordenadas do quadrado
intervaloLat = [-3, -2];%[-2.9580, -2.8231];
intervaloLon = [-55,-54];%[-60.0372,-59.9023];
x = [intervaloLon(1), intervaloLon(2), intervaloLon(2), intervaloLon(1), intervaloLon(1)];
y = [intervaloLat(1), intervaloLat(1), intervaloLat(2), intervaloLat(2), intervaloLat(1)];
% Crie o gráfico do quadrado com fundo transparente
figure;
plot(x, y, 'b','LineWidth',2); % 'b' para linhas azuis, você pode alterar a cor conforme necessário
hold on, plot(lon_resultado,lat_resultado)
Does anyone have an alternative for my filtering to stay within the delimited square?

Risposta accettata

Hassaan
Hassaan il 2 Gen 2024
@Augusto Gabriel da Costa Pereira Made some changes. Try this out and let me know. Thanks.
clc; clear;
% Load your latitude and longitude data
load('Lat_and_Lon.mat')
% Define your latitude and longitude boundaries
lat_bounds = [-3, -2]; % Latitude interval
lon_bounds = [-55, -54]; % Longitude interval
% Filter for points within the specified latitude and longitude intervals
lat_filter = (lat_h12v09 >= lat_bounds(1)) & (lat_h12v09 <= lat_bounds(2));
lon_filter = (lon_h12v09 >= lon_bounds(1)) & (lon_h12v09 <= lon_bounds(2));
% Create a combined filter for both latitude and longitude
combined_filter = lat_filter & lon_filter;
% Apply the filter to get the results
lat_resultado = lat_h12v09(combined_filter);
lon_resultado = lon_h12v09(combined_filter);
% Plotting
% Define the corners of the square boundary
x = [lon_bounds(1), lon_bounds(2), lon_bounds(2), lon_bounds(1), lon_bounds(1)];
y = [lat_bounds(1), lat_bounds(1), lat_bounds(2), lat_bounds(2), lat_bounds(1)];
% Create the plot of the square with a transparent background
figure;
plot(x, y, 'b','LineWidth',2); % Blue lines for the square boundary
hold on; % Hold on to plot additional data on the same figure
scatter(lon_resultado, lat_resultado, 'r.'); % Plot the filtered points in red
% Additional plot formatting
xlabel('Longitude');
ylabel('Latitude');
title('Filtered Lat/Lon Points within Specified Bounds');
legend('Boundary', 'Filtered Points');
grid on; % Optional: Add a grid to the plot for better readability
% Check the results
disp('Number of points within the boundary:');
disp(sum(combined_filter)); % Display the count of points within the boundary
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  4 Commenti
Hassaan
Hassaan il 2 Gen 2024
if lon_filter is a 2D matrix and you want to find columns where all the elements are 1, your current approach should work:
LonIndex = find(all(lon_filter, 1)); % find columns where all elements are true
And if you want to find rows where all the elements are 1, you would use:
LatIndex = find(all(lat_filter, 2)); % find rows where all elements are true
If you want to find any rows or columns that contain at least one element with the value 1, you would not use all; instead, you would use any:
LonIndexAny = find(any(lon_filter, 1)); % find columns that contain at least one '1' LatIndexAny = find(any(lat_filter, 2)); % find rows that contain at least one '1'
Ensure that your logical indexing is set up correctly to match the dimensions of your data. If you're dealing with a non-standard data structure or a more complex filtering condition, you may need to adjust your approach accordingly. If lon_filter is not giving you the expected results, verify that the matrix is correctly oriented and that you are using the correct dimension in the all or any function.
Augusto Gabriel da Costa Pereira
I think I've arrived at a satisfactory result:
[LonIndex, ~] = find(any(lon_filter == 1, 2));
[~, LatIndex] = find(any(lat_filter == 1, 1));

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by