%% Nearest Neighbor Interpolation
% Use nearest neighbor interpolation on a 3-D data set.
%
% Create a sample 3-D data set. The matrix  X contains the xyz locations of the observed data, and v contains the (randomly generated) observed data. This type of data set could represent, for example, oxygen levels in ocean water at these locations.

X = [rand(100,1) rand(100,1) rand(100,1)];
v = rand(100,1);

%
% Use nearest neighbor interpolation to approximate the value of the underlying function at some query points.

[xx,yy,zz] = meshgrid(0.2:0.025:0.8);
xq = [xx(:) yy(:) zz(:)];
vq = griddatan(X,v,xq,'nearest');

%
% Plot slices of the result on top of the sample point locations.

vq = reshape(vq,size(xx));
plot3(X(:,1),X(:,2),X(:,3),'r*')
hold on
slice(xx,yy,zz,vq,[0.2 0.4 0.6 0.8],0.5,0.5)

% Copyright 2012 The MathWorks, Inc.