Is there anyway to Re-grid one dataset to another dataset resolution without interpolating??

5 vues (au cours des 30 derniers jours)
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 commentaires
Subhodh Sharma
Subhodh Sharma le 2 Août 2021
Modifié(e) : Subhodh Sharma le 3 Août 2021
@Scott MacKenzie, thanks for your response. It's a big data, I can't post the data.
The original data has Lon=-178.5:3:178.5; Lat=-89:2:89; Data= rand(120,90)
Resolution of lon and lat where I want to Re-grid is.. lon=-179.75:0.5:179.75; lat=-89.75:0.5:89.75
*I tried griddatainterpolant. Pls don't suggest the same thing. I don't want to interpolate.
Walter Roberson
Walter Roberson le 3 Août 2021
With that range of latitudes, you need to more carefully define "closest".

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Août 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)
ans = 1×5
6 18 30 42 54
ans = 1×5
4 12 20 28 36
B = nan(nnlon, nnlat);
B(lonbin, latbin) = A;
A(1:3,1:3)
ans = 3×3
0.3590 0.6610 0.3157 0.8919 0.9022 0.2011 0.5665 0.0365 0.8013
B(1:20,1:20)
ans = 20×20
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.3590 NaN NaN NaN NaN NaN NaN NaN 0.6610 NaN NaN NaN NaN NaN NaN NaN 0.3157 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
  3 commentaires
Walter Roberson
Walter Roberson le 3 Août 2021
Modifié(e) : Walter Roberson le 4 Août 2021
I do not interpolate the data: I interpolate indices. It is a method that works for regular rectangular grids, but does not require that the values in the source or destination are equally spaced, but does require that "nearest" in a dimension can be decided by referring to only that dimension. The interp1 code can be replaced with discretize() with some additional work to find the centers.
In the case where the spacing is guaranteed to be regular, the indices can be calculated more directly using rounding.
In your general case, what aspects will always be true, and what aspects can vary? For example if the grid spacings will always be what you show here but the grid boundaries could vary, then we might write optimal code differently than if the grid boundaries are fixed but the spacings might vary.
In your situation, is it certain to be true that intersections in the old grid will always map *exactly* to an intersection in the new grid? For example the spacing might be 4 times finer, but the edges have been choosen so that everything lines up? If so then the transfer can end up being simple.
Subhodh Sharma
Subhodh Sharma le 4 Août 2021
@Walter Roberson, thanks you are right. I understood your point. Your code worked great. Thanks .
I really appreciate your kind help.

Connectez-vous pour commenter.

Plus de réponses (2)

darova
darova le 2 Août 2021
Maybe this
A = ones(3,3);
B = [A;nan(6,3)];
C = reshape(B,3,[])
C = 3×9
1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN 1 NaN NaN

John D'Errico
John D'Errico le 3 Août 2021
Modifié(e) : John D'Errico le 3 Août 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)
A = 4×3
0.7022 0.3936 0.9632 0.8409 0.3974 0.6029 0.0568 0.3554 0.9848 0.1011 0.3568 0.5405
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
B = 9×7
0.7022 NaN NaN 0.3936 NaN NaN 0.9632 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.8409 NaN NaN 0.3974 NaN NaN 0.6029 NaN NaN NaN NaN NaN NaN NaN 0.0568 NaN NaN 0.3554 NaN NaN 0.9848 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.1011 NaN NaN 0.3568 NaN NaN 0.5405
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.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by