Is there anyway to Re-grid one dataset to another dataset resolution without interpolating??
5 views (last 30 days)
Show older comments
Subhodh Sharma
on 2 Aug 2021
Edited: Walter Roberson
on 4 Aug 2021
Hey everyone, I have a geographical data (A) of dimension (120(lon) × 90(lat)) whose longitude (start=-178.5, end=178.5) resolution is 3 deg and for lat (start=-89, end=89) is 2 deg.I want to Re-grid this data to a different dimension of (720×360) (whose lat_start=-89.75,lat_end=89.75 and lon_start=-179.75, lon_end=179.75) whose lat and lon resolution is 0.5 deg without interpolating. Without interpolating means I want to put original data (A) every point to the closest of the new resolution (lat,lon), otherpoints I want to put zero or NaN. So the new matrix will have new dimension (720 × 360).
3 Comments
Walter Roberson
on 3 Aug 2021
With that range of latitudes, you need to more carefully define "closest".
Accepted Answer
Walter Roberson
on 2 Aug 2021
origLon=-178.5:3:178.5; origLat=-89:2:89;
norlon = length(origLon); norlat = length(origLat);
A = rand(norlon,norlat);
newLon=-179.75:0.25:179.75; newLat=-89.75:0.25:89.75;
nnlon = length(newLon); nnlat = length(newLat);
lonbin = interp1(newLon, 1:nnlon, origLon, 'nearest');
latbin = interp1(newLat, 1:nnlat, origLat, 'nearest');
lonbin(1:5), latbin(1:5)
B = nan(nnlon, nnlat);
B(lonbin, latbin) = A;
A(1:3,1:3)
B(1:20,1:20)
More Answers (2)
John D'Errico
on 3 Aug 2021
Edited: John D'Errico
on 3 Aug 2021
What you are asking to do is arguably still "interpolation" of a sort. In fact, Walter''s use of nearest neighbor is quite reasonable.
At the same time, what you are asking to do is really laughably trivial to accomplish, just using a simple index rescaling. For example:
A = rand(4,3)
Now, suppose I want to "regrid" this onto a 9x7 grid, inserting NaNs wherever necessary.
OldSize = size(A);
NewSize = [9,7];
B = NaN(NewSize);
[indr,indc] = ndgrid(1:OldSize(1),1:OldSize(2));
% convert to the new indexing
indr = 1 + round((indr-1)*(NewSize(1)-1)/(OldSize(1)-1));
indc = 1 + round((indc-1)*(NewSize(2)-1)/(OldSize(2)-1));
B(sub2ind(NewSize,indr,indc)) = A
The result here is a bit coarse, with either 1 or 2 NaNs inserted in some rows as needed.
This will work of couse as long as the new grid is larger than the old one. If it was smaller, then you have a serious problem, one that cannot be resolved directly using any such scheme. But then you would need to decide what it means to regrid onto a smaller grid. You might decide to average elements that end up in the same boxes. This is itself doable, using tools like accumarray.
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!