Select data values from within a range

조회 수: 7 (최근 30일)
Robert
Robert 2019년 1월 13일
답변: Akshat 2025년 1월 30일
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.

답변 (1개)

Akshat
Akshat 2025년 1월 30일
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!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by