ZI = interp2(X,Y,Z,XI,YI)ZI = interp2(X,Y,Z,XI,YI,'method')
interp2 interpolates between data points. It finds values of a two-dimensional function underlying the data at intermediate points.
ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to the elements of XI and YI and determined by interpolation within the two-dimensional function specified by matrices X, Y, and Z. Matrix Z contains the values of a two-dimensional function at the corresponding abscissae contained in matrices X and Y.
X can be a row vector, in which case the elements are assumed to apply to the columns of Z. Similarly, Y can be a column vector and its elements are assumed to apply across the rows of Z.
Interpolation is the same operation as table lookup. Described in table lookup terms, the table is tab = [NaN,Y; X,Z] and interp2 looks up the elements of XI in X, YI in Y, and, based upon their location, returns values ZI interpolated within the elements of Z.
ZI = interp2(X,Y,Z,XI,YI,'method') specifies alternative interpolation methods, where method can be:
'linear' for linear interpolation (default)
'cubic' for cubic interpolation
All interpolation methods require that X and Y be monotonic. The 'cubic' method also requires that X and Y contain uniformly spaced points.
peaks function over a finer grid:
[X,Y] = meshgrid(-3:.25:3);Z = peaks(X,Y);[XI,YI] = meshgrid(-3:.125:3);ZI = interp2(X,Y,Z,XI,YI);mesh(X,Y,Z), hold, mesh(XI,YI,ZI+15)hold offaxis([-3 3 -3 3 -5 20])
If Poptable is the 6-by-4 table:
Then the statementsPoptable =0. 10.000 20.000 30.0001950. 150.697 199.592 187.6251960. 179.323 195.072 250.2871970. 203.212 179.092 322.7671980. 226.505 153.706 426.7301990. 249.633 120.281 598.243
extract ayears = Poptable(2:6,1);values = Poptable(1,2:4);T = poptable(2:6,2:4)
years vector (the first column of the table), a values vector (the first row of the table), and a matrix T containing all the population data in the table (everything except the first row and the first column).Now the statement
p = interp2(values,years,T,15,1975)
returns
p =190.629
This is the average of the four table values
203.212 179.092226.505 153.706
griddata,interp1,interpft
(c) Copyright 1994 by The MathWorks, Inc.