Remove 2D array elements outside of a range
7 次查看(过去 30 天)
显示 更早的评论
Hi,
I am attempting to remove array elements that fall outside of specified ranges. The array I am working with is 199x199 (wmax), and there are subsequent 199x199 arrays that contain both longitudes (x_pts) and latitudes (y_pts).
Essentially I want to create a smaller array that only includes data within 2 given latitudes and longitudes. Whenever I try this, it returns an N x 1 array, but I am hoping for a 2D array to be returned.
wmax(x_pts>-103&x_pts<-100&y_pts>34.75&y_pts<36.5;
0 个评论
回答(1 个)
Voss
2022-3-30
Try one of these two things, depending on whether x_pts corresponds to the rows or columns of wmax:
% if x_pts corresponds to the rows of wmax and y_pts is the columns:
wmax(x_pts>-103 & x_pts<-100, y_pts>34.75 & y_pts<36.5);
% if x_pts corresponds to the columns of wmax and y_pts is the rows:
wmax(y_pts>34.75 & y_pts<36.5, x_pts>-103 & x_pts<-100);
Example:
x_pts = 1:10;
y_pts = 102:109;
% x_pts goes with rows, y_pts goes with columns, in this case
wmax = randn(numel(x_pts),numel(y_pts))
wmax(x_pts>3 & x_pts<7, y_pts>105 & y_pts<108)
2 个评论
Voss
2022-5-28
Make sure you have the comma where you had an & before:
wmax(x_pts>-103 & x_pts<-100, y_pts>34.75 & y_pts<36.5);
% ^
If that's not the problem, then can you demonstrate it here, with an example? Either construct a small matrix along with two vectors to use for indexing into the matrix, or save your variables to a mat file and upload it (with the paperclip button).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!