MATLAB Answers

I need correct syntax of interpolation in this case

29 views (last 30 days)
Hello, I have a 40x32x360 double array. (latitude x longitude x time) which presents precipitation for these latitudes and longitude for 360 months.
I want to interpolate this array to found how much the precipitation value for latitude = 30.3772 and longitude 38.2147 in all 360 months (the output is 1x1x360 array).
I read about interp2 and interp3 but I don't know what is suitable in this case and how to use them. Can you tell me what is the true syntax in order to do this?


Sign in to comment.

Accepted Answer

S. Walter
S. Walter on 5 May 2020
You'll want to make sure your latitude, longitude, and time are all 40x32x360 arrays. For that you can use ndgrid ( if they are not.
In your case, you'd want something along these lines:
% Create a interpolation surface that uses latitude, longitude,
% and time to interpolate over precipitation
F = griddedInterpolant(lat,long,t,precipitation)
% Pick the time of interest
tOfInterest = [1:1:360]';
% Pick the latitude and longitude of interest but make sure
% it's the same size as your time vector
latOfInterest = 30.3772*ones(size(tOfInterest));
longOfInterest = 38.2147*ones(size(tOfInterest));
% Now interpolate to those values
AnswerYouAreLookingFor = F(latOfInterest,longOfInterest,tOfInterest)
Hope that helps!


Behzad Navidi
Behzad Navidi on 5 May 2020
Dear S. Walter
I attached my data, unfortunately it gives me an error that says Grid arrays must have NDGRID structure.
I think my arrays are ndgrided, can you look at my data and help me with this issue?
Thank you so much for your help
S. Walter
S. Walter on 5 May 2020
Your data is in vectors, not arrays. You will have to use ndgrid to convert it from vectors to arrays.
You can check your variables by using the "whos" command:
and the output you'll get is:
Name Size Bytes Class Attributes
dates 360x1 2880 datetime
lati 1x32 128 single
loni 1x40 160 single
precipitation 40x32x360 1843200 single
If you look at the "Size" column, it shows you "dates" is 360x1, "lati" is 1x32, and "loni" is 1x40. But what you're interested in interpolating over is 40x32x360. The interpolator doesn't know how to organize those vectors into a matrix, the programmer will have to do that. In order to create that matrix, you use the ndgrid function.
Looking at your data though, you can see precipitation is 40x32x360 - meaning it's organized as longitude by latitude by date. When you go to use ndgrid, you have to keep that same ordering
[x,y,z] = ndgrid(loni,lati,dates);
When you do that, you should see that x, y, and z are now the same size as precipitation. When you then organize your griddedInterpolant, you will have to keep the same order
F = griddedInterpolant(x,y,z,precipitation)
But Wait! The other part of your problem is that, looking at the "Class" column of your variables - dates is in datetime format. The interpolator is not set up to handle classes other than single or double. So you'll have to find a way to convert the "dates" variable into a numerical value. I can't help you how to do that, it depends on the rest of your code.
Lastly! Make sure when you go to use the interpolator you just created, you always keep the same order - longitude, latitude, dates - otherwise you will receive an erroneous answer.
Behzad Navidi
Behzad Navidi on 6 May 2020
Dear S. Walter,
I really appreciate you, you are really helped me, Thanks.

Sign in to comment.

More Answers (0)




Translated by