I have a set of data for example:
X Y Z
1 3 7
2 5 8
1 4 9
3 6 10
I would like to interpolate Z
for X=2.5
and Y=3.5
. How do i do it? I cannot use interp2
here because X
and Y
are not strictly monotonic (increasing or decreasing).
Interpolation means determining a value from the existing values in a given data set. Another way of describing it is the act of inserting or interjecting an intermediate value between two other values.
Linear interpolation is often used to regrid evenly-spaced data, such as longitude / latitude gridded data, to a higher or lower resolution.
Based on the given data set, farmers can estimate the height of trees for any number of days until the tree reaches its normal height. For example, based on the above data, the farmer wants to know the tree's height on the 7th day. He can find it out by interpolating the above values.
There are several formal kinds of interpolation, including linear interpolation, polynomial interpolation, and piecewise constant interpolation.
The currently preferred way to perform scattered data interpolation is via the scatteredInterpolant
object class:
>> F = scatteredInterpolant([1 2 1 3].',[3 5 4 6].',[7 8 9 10].','linear') %'
F =
scatteredInterpolant with properties:
Points: [4x2 double]
Values: [4x1 double]
Method: 'linear'
ExtrapolationMethod: 'linear'
>> Zi = F(2.5,3.5)
Zi =
6.7910
Alternate syntax,
>> P = [1 3 7; 2 5 8; 1 4 9; 3 6 10];
>> F = scatteredInterpolant(P(:,1:2),P(:,3),'linear')
For the advantages of scatteredInterpolant
over griddata
see this MathWorks page on Interpolating Scattered Data. In addition to syntactic differences, two main advantages are extrapolation and natural-neighbor interpolation. If you want to interpolate new data with the same interpolant, then you also have the performance advantage of reusing the triangulation computed when the interpolant object is created.
It seems like griddata
is the function you are looking for:
z = griddata( [1 2 1 3], [3 5 4 6], [7 8 9 10], 2.5, 3.5, 'nearest' )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With