Problem in using interp2

Hello,
I want to interpolate my latitude longitude values. They are in the form of matrix.
Lat = 406x270;
Lon = 406X270;
I have read docs about it but still can't figure out that how to do interpolation. Vq = interp2(X,Y,V,Xq,Yq). How to use V here? What is meant by 'Xq and Yq contain the coordinates of the query points'?
I have another variable M (2030x1354). So, I want Lat Lon to become:
Lat1 = 2030x1354;
Lon1 = 2030x1354;
So, should I use M instead of V? Then what about Xq and Yq?
After going through some questions I tried using meshgrid for it. But it gave me following error;
Error using repmat
Requested 109620x109620 (44.8GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time
and cause MATLAB to become unresponsive.
Lat Lon data is attached. Is there anyother way to do interpolation? Thank you.

Answers (1)

KSSV
KSSV on 1 Oct 2021
Edited: KSSV on 1 Oct 2021
Vq = interp2(X,Y,V,Xq,Yq) ;
In the above V is the value at (X,Y). You need to have that value.It seems you have only Lon and Lat here.
% Demo
[X,Y,Z] = peaks(100) ; % Z is here V
% Do interpolation
x0 = min(X(:)) ; x1 = max(X(:)) ;
y0 = min(Y(:)) ; y1 = max(Y(:)) ;
x = linspace(x0,x1,50) ;
y = linspace(y0,y1,50) ;
[Xq,Yq] = meshgrid(x,y) ;
Zq = interp2(X,Y,Z,Xq,Yq) ;
So get your V i.,e. Z i.e. respective Lon, LAt value.

11 Comments

My M variable is 2030x1354. And I want Lat Lon to be of same size.
So, can I use M in place of V?
Yes your M is V.
For Xq and Yq, using meshgrid gives this error:
[Xq,Yq] = meshgrid(lat,lon)
Error using repmat
Requested 109620x109620 (44.8GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time
and cause MATLAB to become unresponsive.
Error in meshgrid (line 61)
xx = repmat(xrow,size(ycol));
It seems you are using meshgrid on a matrix. Show us your whole code.
@KSSV, here is the code;
M = hdfread(hdf_filename, 'Optical_Properties'); % 2030X1354 double
lat = hdfread(hdf_filename, 'Latitude'); % 406x270 double
lon = hdfread(hdf_filename, 'Longitude'); % 406x270 double
[Xq,Yq] = meshgrid(lat,lon) ;
Zq = interp2(lat,lon,M,Xq,Yq) ;
Error:
Error using repmat
Requested 109620x109620 (44.8GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time
and cause MATLAB to become unresponsive.
Error in meshgrid (line 61)
xx = repmat(xrow,size(ycol));
Error in Untitled12 (line 75)
[Xq,Yq] = meshgrid(lat,lon) ;
M = hdfread(hdf_filename, 'Optical_Properties'); % 2030X1354 double
lat = hdfread(hdf_filename, 'Latitude'); % 406x270 double
lon = hdfread(hdf_filename, 'Longitude'); % 406x270 double
[m,n] = size(M) ;
x0 = min(lon(:)) ; x1 = max(lon(:)) ;
y0 = min(lat(:)) ; y1 = max(lat(:)) ;
x = linspace(x0,x1,m) ;
y = linspace(y0,y1,n) ;
[X,Y] = meshgrid(x,y) ;
Z = interp2(X,Y,M',lon,lat)' ;
pcolor(lon,lat,Z') ;
shading interp ;
colorbar
@KSSV, why the output 'Z' is 270x406 double?
Because Xq, Yq is of that size.
@KSSV My purpose of doing interpolation is that I have a text file. Text file contains 2 column vectors (having lat and lon values). So,
lat_txt = 3798x1 double;
lon_txt = 3798x1 double;
I want to use these lat_txt and lon_txt to match with values of lat and lon (from hdf file). And for all the lat and lon values which are equal, extract the value of M (from hdf file).
Sorry, I really don't understand how the output 'Z (270X406)' will do here.
KSSV
KSSV on 1 Oct 2021
Edited: KSSV on 1 Oct 2021
Then attach the text file.
I've attached the text file.

Sign in to comment.

Asked:

IMC
on 1 Oct 2021

Commented:

IMC
on 1 Oct 2021

Community Treasure Hunt

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

Start Hunting!