Select data values from within a range

12 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by