6

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).

chappjc
  • 30,359
  • 6
  • 75
  • 132
user3222217
  • 63
  • 1
  • 3

2 Answers2

10

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.

chappjc
  • 30,359
  • 6
  • 75
  • 132
6

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' )
Shai
  • 111,146
  • 38
  • 238
  • 371