Remove 2D array elements outside of a range

5 Ansichten (letzte 30 Tage)
KCE
KCE am 30 Mär. 2022
Kommentiert: Voss am 28 Mai 2022
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;

Antworten (1)

Voss
Voss am 30 Mär. 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 = 10×8
0.1245 -1.1668 -0.0475 0.5001 -0.2800 1.3163 0.6479 1.2807 1.5173 0.6925 -1.1350 0.5612 2.2336 0.8672 0.3282 -0.2573 -0.6798 1.2087 1.1384 1.8517 1.2552 -0.5807 -0.5476 1.7234 1.1392 -0.7541 0.5278 -0.1743 -0.3634 -0.0129 -1.1077 -0.0505 -0.9966 0.0368 0.0110 -1.2554 0.4684 -2.0030 2.2644 -0.1099 0.0899 -0.0061 -1.2089 -0.4280 -0.8558 1.5466 -0.7792 0.3896 -0.6317 0.0086 0.9400 0.1565 -0.5707 0.1088 -0.5228 -1.4621 0.1477 0.7005 0.1280 0.1691 -0.8256 0.9573 0.7240 1.3552 0.5469 -0.5682 0.0907 1.2396 -1.5674 -0.7999 -1.1673 0.6018 0.7427 -0.5076 1.3764 -1.8479 -0.4785 0.0112 -1.8359 0.3773
wmax(x_pts>3 & x_pts<7, y_pts>105 & y_pts<108)
ans = 3×2
-0.3634 -0.0129 0.4684 -2.0030 -0.8558 1.5466
  2 Kommentare
KCE
KCE am 27 Mai 2022
I have tried this along with multiple combinations, and they all seem to return a 1D answer.
Voss
Voss am 28 Mai 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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by