Using logical array to extract data without reshape

22 views (last 30 days)
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);
mask = lat_mask & lon_mask;
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:
rowind = (find(sum(mask),1,'first'));
colind = (find(sum(mask,2),1,'first'));
r = sum(mask); c = sum(mask,2);
rr = r(rowind); cc = c(colind);
R = reshape(Values(mask), rr, cc);
Is there easier way to do it?
I wanted to attach the lons lats and Values, but unfortunately, they are too large.
Thanks!
Amit
  3 Comments
Amit Kallush
Amit Kallush on 11 May 2023
Thanks Stephan,
I'm sorry I was not clear enough. I mentioned in the original post that the lons and lats are in the same size of Values, meaning that they are also gridded. All three matrices are 3500X7000.
The lons and lats are in MESHGRID format.
I’m adding here a Drive link so you could look on the data yourself:
https://drive.google.com/drive/folders/1d8cPuWB4KCun6Y0eav-C3BoFqhNMfSvU?usp=sharing
Thanks!
Amit
Stephen23
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.

Sign in to comment.

Accepted Answer

Stephen23
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);
R = Values(lat_mask,lon_mask)

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!