# Using logical array to extract data without reshape

16 views (last 30 days)
Amit Kallush on 10 May 2023
Commented: Amit Kallush on 11 May 2023
Hi all,
I managed to solve the problem, but the solution seemed so unelegant that I'm sure there is a better way.
I have a 3500X7000 matrix called "Values" I also have two other matrices named "lats" and "lons" that are the same size and contain the longitude and latitude values corresponding to each cell in "Values."
Now I want the lon and lat limits of the data to be:
lat_min = 33.97757;
lat_max = 34.22959;
lon_min = -106.9664;
lon_max = -106.6382;
And I used the following to create logical matrices that contain one only in the cells that are within range:
lat_mask = (lats >= lat_min) & (lats <= lat_max);
lon_mask = (lons >= lon_min) & (lons <= lon_max);
Here I got stuck. I wanted to extract the data from "Values" using the mask array, but I couldn't do it while preserving the mask array shape (which should be 25X33). I had to look for the row and columns within "mask" sum them and look for the sum value and then reshape it, and the whole thing looks very unelegant:
rr = r(rowind); cc = c(colind);
Is there easier way to do it?
I wanted to attach the lons lats and Values, but unfortunately, they are too large.
Thanks!
Amit
Stephen23 on 11 May 2023
Edited: Stephen23 on 11 May 2023
"I mentioned in the original post that the lons and lats are in the same size of Values, meaning that they are also gridded."
Yes you wrote that they are the same size, but that does NOT mean that they are gridded: you are confusing the size of an array with the arrangement of data within that array. Not the same thing at all.
For example, here are some lat and lon arrays that are not gridded:
lat = rand(3,5)
lat = 3×5
0.3750 0.8179 0.3697 0.4818 0.3205 0.8124 0.2282 0.0504 0.7717 0.1269 0.6206 0.0234 0.6904 0.5116 0.6178
lon = rand(3,5)
lon = 3×5
0.1039 0.8742 0.7210 0.8718 0.7215 0.6711 0.3077 0.1873 0.2940 0.4561 0.9741 0.5777 0.9875 0.2244 0.8124
Do those arrays have some particular size? Of course. Are they gridded? No.
"The lons and lats are in MESHGRID format."
Thank you, that is the statement we needed.

Stephen23 on 11 May 2023
Edited: Stephen23 on 11 May 2023
Note that saving those huge lats&lons matrices is rather waste of space: you actually only need the first row/column.
"Is there easier way to do it?"
Yes, you are overthinking this:
latV = lats(:,1);
lonV = lons(1,:);
assert(all(diff(latV)),'not MESHGRID format')
assert(all(diff(lonV)),'not MESHGRID format')
lat_mask = (latV >= lat_min) & (latV <= lat_max);
lon_mask = (lonV >= lon_min) & (lonV <= lon_max);
Amit Kallush on 11 May 2023
OK this is making much more sense.
Thank you for the help, that worked
Amit

### Categories

Find more on Geographic Plots in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!