Remove 2D array elements outside of a range
조회 수: 6 (최근 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!