Select data values from within a range

7 views (last 30 days)
Robert
Robert on 13 Jan 2019
Answered: Akshat on 30 Jan 2025
So I have a matrix of about 42000 rows, which give latitudes and longitudes of each entry in columns 7 and 8 respectively. Right now, my script pulls latitudes and longitudes as variables, and then another couple lines select values within a range. My script is below:
lat_r = A(:,7);
long_r = A(:,8);
lat = lat_r(lat_r > x & lat_r < y);
long = long_r(long_r > a & long_r < b);
My problem is that this only lists each value from each individual column that is between the given values (x,y or a,b). What I'd like to do is be able to find which rows have a latitude within the given range and then check if the corresponding longitude is also within the given range, then enter these values into a nx2 matrix.
This is probably a quick answer, but I'm fairly new working with MATLAB so any help would be greatly appreciated.

Answers (1)

Akshat
Akshat on 30 Jan 2025
The issue mentioned by you can be tackled using logical indexing, which is supported by MATLAB. In this, you basically get the indices where both the conditions are true.
Following I have modified your code to get the indices with both the conditions true.
lat_r = A(:, 7);
long_r = A(:, 8);
valid_indices = (lat_r > x & lat_r < y) & (long_r > a & long_r < b);
valid_lat_long = A(valid_indices, [7, 8]);
disp(valid_lat_long);
Hope this helps!

Community Treasure Hunt

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

Start Hunting!