How to interpolate/regrid 2-D array
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gonzalo Ferrada
am 21 Mär. 2020
Bearbeitet: Image Analyst
am 24 Mär. 2020
Hello,
I am trying to regrid data using a cubic interpolation. I have a 2-D matrix (var_in) with 2 coordinates (x_in and y_in), see the plot below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278594/image.png)
I have created the two target 2-D coordinates (x_out and y_out), both with the same dimensions and monotonic. First, I tried using griddata, which I have used for other purposes and works well. However, in this case it did not produce the expected results, see the figure below. (I requested the limit up to 15000 in y_out).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278595/image.png)
You can notice the original data (var_in) varies from 0 to 1, while griddata produced a variable with very different limits.
Also, I tried using scatteredInterpolant by working around my data, but again it did not produce reasonable results:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278596/image.png)
As you can see, none of the two methods did not produce results similar to the original data. I tried to use interp2 and interpn but they did not work. I guess it is because x_in and y_in are not meshgrids.
Does anyone have a suggestion on how to solve this?
I am attaching a mat file that includes the original data, its coordinates and the requested output coordinates: x_in, y_in, var_in, x_out, y_out.
Thanks.
3 Kommentare
Image Analyst
am 24 Mär. 2020
Bearbeitet: Image Analyst
am 24 Mär. 2020
Why is x_in a 2-D matrix? I thought it was just a 1-D list of x coordinates, and you had a matching/corresponding list of y values, then you had some data value for each (x,y) pair. Please explain what this all represents:
struct with fields:
var_in: [52×44 single]
x_in: [52×44 double]
x_out: [289×775 double]
y_in: [52×44 single]
y_out: [289×775 single]
Did you already run x and y through meshgrid() to get x_in and y_in? If so, do you have the original x and y. I guess I could get it using unique() if I had to.
Akzeptierte Antwort
darova
am 23 Mär. 2020
Your X and Y variable of very different scales
>> max(x_in(:)) - min(x_in(:))
ans =
5.3750
>> max(y_in(:)) - min(y_in(:))
ans =
2.0106e+04
I tried to scale your X variable
load data_sample.mat
X = double(x_in);
Y = double(y_in);
Z = double(var_in);
x1 = linspace(min(X(:)),max(X(:)),200);
y1 = linspace(min(Y(:)),max(Y(:)),200);
[X1,Y1] = meshgrid(x1,y1);
scale = (max(Y(:))-min(Y(:)))/(max(X(:))-min(X(:)));
Z1 = griddata(X*scale,Y,Z,X1*scale,Y1,'linear');
subplot(121)
surf(X,Y,Z,'edgecolor','none')
view(2)
axis tight
subplot(122)
surf(X1,Y1,Z1,'edgecolor','none')
view(2)
axis tight
linear interpolation cubic interpolation
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278947/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278948/image.png)
4 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!