Main Content

scatteredInterpolant

Interpolate 2-D or 3-D scattered data

Description

UsescatteredInterpolantto perform interpolation on a 2-D or 3-D data set ofscattered data.scatteredInterpolantreturns theinterpolantFfor the given data set. You can evaluateFat a set of query points, such as(xq,yq)in 2-D, to produce interpolated valuesvq = F(xq,yq).

UsegriddedInterpolantto perform interpolation withgridded data.

Creation

Description

F= scatteredInterpolantcreates an empty scattered data interpolant object.

example

F= scatteredInterpolant(x,y,v)creates an interpolant that fits a surface of the formv=F(x,y). Vectorsxandyspecify the(x,y)coordinates of the sample points.vis a vector that contains the sample values associated with the points(x,y).

example

F= scatteredInterpolant(x,y,z,v)creates a 3-D interpolant of the formv=F(x,y,z).

example

F= scatteredInterpolant(P,v)specifies the coordinates of the sample points as an array. The rows ofPcontain the (x,y) or (x,y,z) coordinates for the values inv.

example

F= scatteredInterpolant(___,Method)specifies an interpolation method:'nearest','linear', or'natural'. SpecifyMethodas the last input argument in any of the first three syntaxes.

example

F= scatteredInterpolant(___,Method,ExtrapolationMethod)specifies both the interpolation and extrapolation methods. PassMethodandExtrapolationMethodtogether as the last two input arguments in any of the first three syntaxes.

  • Methodcan be:'nearest','linear', or'natural'.

  • ExtrapolationMethodcan be:'nearest','linear', or'none'.

Input Arguments

expand all

Sample points, specified as vectors of the same size asv. The sample points should be unique. However, if the sample points contain duplicates,scatteredInterpolantdisplays a warning and merges the duplicates into a single point.

Data Types:double

Sample points array, specified as anm-by-nmatrix, wheremis the number of points andnis the dimension of the space where the points reside. Each row ofPcontains the (x,y) or (x,y,z) coordinates of a sample point. The sample points should be unique. However, if the sample points contain duplicates,scatteredInterpolantdisplays a warning and merges the duplicates into a single point.

Data Types:double

Sample values, specified as a vector that defines the function values at the sample points,v=F(x,y,z).

scatteredInterpolantdoes not ignoreNaNvalues inv, so interpolation results near those sample points are alsoNaN.

Data Types:double

Interpolation method, specified as one of these options.

Method Description Continuity
'linear'(default)

Linear interpolation

C0
'nearest'

Nearest neighbor interpolation

Discontinuous
'natural'

Natural neighbor interpolation

C1(except at sample points)

Extrapolation method, specified as one of these options.

ExtrapolationMethod Description
'linear'

Linear extrapolation based on boundary gradients. Default whenMethodis'linear'or'natural'.

'nearest'

Nearest neighbor extrapolation. This method evaluates to the value of the nearest neighbor on the boundary. Default whenMethodis'nearest'.

'none'

No extrapolation. Any queries outside the convex hull ofPointsreturnNaN.

Properties

expand all

Sample points, specified as a matrix. The size of the matrix ism-by-2orm-by-3to representmpoints in 2-D or 3-D space. Each row ofPointscontains the (x,y) or (x,y,z) coordinates of a unique sample point. The rows inPointscorrespond to the function values inValues.

Data Types:double

Function values at sample points, specified as a vector of values associated with each point inPoints.

scatteredInterpolantdoes not ignoreNaNvalues inValues, so interpolation results near those sample points are alsoNaN.

Data Types:double

Interpolation method, specified as'linear','nearest', or'natural'. SeeMethodfor descriptions of these methods.

Extrapolation method, specified as'nearest','linear', or'none'. SeeExtrapolationMethodfor descriptions of these methods.

Data Types:double

Usage

Description

UsescatteredInterpolantto create theinterpolant,F. Then you can evaluateFat specific points using any of the following syntaxes:

Vq = F(Pq)specifies query points in the matrixPq. Each row inPqcontains the coordinates of a query point.

Vq = F(Xq,Yq)andVq = F(Xq,Yq,Zq)specify query points as two or three matrices of equal size.

Vq = F({xq,yq})andVq = F({xq,yq,zq})specify query points asgrid vectors. Use this syntax to conserve memory when you want to query a large grid of points.

Examples

collapse all

Define some sample points and calculate the value of a trigonometric function at those locations. These points are the sample values for the interpolant.

t = linspace(3/4*pi,2*pi,50)'; x = [3*cos(t); 2*cos(t); 0.7*cos(t)]; y = [3*sin(t); 2*sin(t); 0.7*sin(t)]; v = repelem([-0.5; 1.5; 2],length(t));

Create the interpolant.

F = scatteredInterpolant(x,y,v);

Evaluate the interpolant at query locations (xq,yq).

tq = linspace(3/4*pi+0.2,2*pi-0.2,40)'; xq = [2.8*cos(tq); 1.7*cos(tq); cos(tq)]; yq = [2.8*sin(tq); 1.7*sin(tq); sin(tq)]; vq = F(xq,yq);

Plot the result.

plot3(x,y,v,'.',xq,yq,vq,'.'), gridontitle('Linear Interpolation') xlabel('x'), ylabel('y'), zlabel('Values') legend('Sample data','Interpolated query data','Location','Best')

Figure contains an axes object. The axes object with title Linear Interpolation contains 2 objects of type line. These objects represent Sample data, Interpolated query data.

Create an interpolant for a set of scattered sample points, then evaluate the interpolant at a set of 3-D query points.

Define 200 random points and sample a trigonometric function. These points are the sample values for the interpolant.

rngdefault; P = -2.5 + 5*rand([200 3]); v = sin(P(:,1).^2 + P(:,2).^2 + P(:,3).^2)./(P(:,1).^2+P(:,2).^2+P(:,3).^2);

Create the interpolant.

F = scatteredInterpolant(P,v);

Evaluate the interpolant at query locations (xq,yq,zq).

[xq,yq,zq] = meshgrid(-2:0.25:2); vq = F(xq,yq,zq);

Plot slices of the result.

xslice = [-.5,1,2]; yslice = [0,2]; zslice = [-2,0]; slice(xq,yq,zq,vq,xslice,yslice,zslice)

Figure contains an axes object. The axes object contains 7 objects of type surface.

Replace the elements in theValuesproperty when you want to change the values at the sample points. You get immediate results when you evaluate the new interpolant because the original triangulation does not change.

Create 50 random points and sample an exponential function. These points are the sample values for the interpolant.

rng('default') x = -2.5 + 5*rand([50 1]); y = -2.5 + 5*rand([50 1]); v = x.*exp(-x.^2-y.^2);

Create the interpolant.

F = scatteredInterpolant(x,y,v)
F = scatteredInterpolant with properties: Points: [50x2 double] Values: [50x1 double] Method: 'linear' ExtrapolationMethod: 'linear'

Evaluate the interpolant at(1.40,1.90).

F(1.40,1.90)
ans = 0.0069

Change the interpolant sample values and reevaluate the interpolant at the same point.

vnew = x.^2 + y.^2; F.Values = vnew; F(1.40,1.90)
ans = 5.6491

Usegroupsummaryto eliminate duplicate sample points and control how they are combined prior to callingscatteredInterpolant.

Create a 200-by-3 matrix of sample point locations. Add duplicate points in the last five rows.

P = -2.5 + 5*rand(200,3); P(197:200,:) = repmat(P(196,:),4,1);

Create a vector of random values at the sample points.

V = rand(size(P,1),1);

If you attempt to usescatteredInterpolant重复采样点,它会抛出一个警告and averages the corresponding values inVto produce a single unique point. However, you can usegroupsummaryto eliminate the duplicate points prior to creating the interpolant. This is particularly useful if you want to combine the duplicate points using a method other than averaging.

Usegroupsummaryto eliminate the duplicate sample points and preserve the maximum value inVat the duplicate sample point location. Specify the sample points matrix as the grouping variable and the corresponding values as the data.

[V_unique,P_unique] = groupsummary(V,P,@max);

Since the grouping variable has three columns,groupsummaryreturns the unique groupsP_uniqueas a cell array. Convert the cell array back into a matrix.

P_unique = [P_unique{:}];

Create the interpolant. Since the sample points are now unique,scatteredInterpolantdoes not throw a warning.

I = scatteredInterpolant(P_unique,V_unique);

Compare the results of several different interpolation algorithms offered byscatteredInterpolant.

Create a sample data set of 50 scattered points. The number of points is artificially small to highlight the differences between the interpolation methods.

x = -3 + 6*rand(50,1); y = -3 + 6*rand(50,1); v = sin(x).^4 .* cos(y);

Create the interpolant and a grid of query points.

F = scatteredInterpolant(x,y,v); [xq,yq] = meshgrid(-3:0.1:3);

Plot the results using the'nearest','linear', and'natural'方法。每一次插值方法改变s, you need to requery the interpolant to get the updated results.

F.Method ='nearest'; vq1 = F(xq,yq); plot3(x,y,v,'mo') holdonmesh(xq,yq,vq1) title('Nearest Neighbor') legend('Sample Points','Interpolated Surface','Location','NorthWest')

Figure contains an axes object. The axes object with title Nearest Neighbor contains 2 objects of type line, surface. These objects represent Sample Points, Interpolated Surface.

F.Method ='linear'; vq2 = F(xq,yq); figure plot3(x,y,v,'mo') holdonmesh(xq,yq,vq2) title('Linear') legend('Sample Points','Interpolated Surface','Location','NorthWest')

Figure contains an axes object. The axes object with title Linear contains 2 objects of type line, surface. These objects represent Sample Points, Interpolated Surface.

F.Method ='natural'; vq3 = F(xq,yq); figure plot3(x,y,v,'mo') holdonmesh(xq,yq,vq3) title('Natural Neighbor') legend('Sample Points','Interpolated Surface','Location','NorthWest')

Figure contains an axes object. The axes object with title Natural Neighbor contains 2 objects of type line, surface. These objects represent Sample Points, Interpolated Surface.

Plot the exact solution.

figure plot3(x,y,v,'mo') holdonmesh(xq,yq,sin(xq).^4 .* cos(yq)) title('Exact Solution') legend('Sample Points','Exact Surface','Location','NorthWest')

Figure contains an axes object. The axes object with title Exact Solution contains 2 objects of type line, surface. These objects represent Sample Points, Exact Surface.

Query an interpolant at a single point outside the convex hull using nearest neighbor extrapolation.

Define a matrix of 200 random points and sample an exponential function. These points are the sample values for the interpolant.

rng('default') P = -2.5 + 5*rand([200 2]); x = P(:,1); y = P(:,2); v = x.*exp(-x.^2-y.^2);

Create the interpolant, specifying linear interpolation and nearest neighbor extrapolation.

F = scatteredInterpolant(P,v,'linear','nearest')
F = scatteredInterpolant with properties: Points: [200x2 double] Values: [200x1 double] Method: 'linear' ExtrapolationMethod: 'nearest'

Evaluate the interpolant outside the convex hull.

vq = F(3.0,-1.5)
vq = 0.0029

禁用外推和评估Fat the same point.

F.ExtrapolationMethod ='none'; vq = F(3.0,-1.5)
vq = NaN

More About

expand all

Tips

  • It is quicker to evaluate ascatteredInterpolantobjectFat many different sets of query points than it is to compute the interpolations separately using the functionsgriddataorgriddatan. For example:

    % Fast to create interpolant F and evaluate multiple timesF = scatteredInterpolant(X,Y,V) v1 = F(Xq1,Yq1) v2 = F(Xq2,Yq2)% Slower to compute interpolations separately using griddatav1 = griddata (X, Y, V, Xq1 Yq1) v2 = griddata (X, Y, V, Xq2,Yq2)
  • To change the interpolation sample values or interpolation method, it is more efficient to update the properties of the interpolant objectFthan it is to create a newscatteredInterpolantobject. When you updateValuesorMethod, the underlying Delaunay triangulation of the input data does not change, so you can compute new results quickly.

  • Scattered data interpolation withscatteredInterpolantuses a Delaunay triangulation of the data, so can be sensitive to scaling issues in the sample pointsx,y,z, orP. When this occurs, you can usenormalizeto rescale the data and improve the results. SeeNormalize Data with Differing Magnitudesfor more information.

Algorithms

scatteredInterpolantuses a Delaunay triangulation of the scattered sample points to perform interpolation[1].

References

[1] Amidror, Isaac. “Scattered data interpolation methods for electronic imaging systems: a survey.”Journal of Electronic Imaging. Vol. 11, No. 2, April 2002, pp. 157–176.

Extended Capabilities

已经rsion History

Introduced in R2013a