Remove 2D array elements outside of a range
5 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Voss
on 30 Mar 2022
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 Comments
Voss
on 28 May 2022
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).
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!