%% Eliminate Duplicate Sample Points
% Use groupsummary to eliminate duplicate sample points and control how they are combined prior to calling scatteredInterpolant.
%
% 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 use scatteredInterpolant with duplicate sample points, it throws a warning and averages the corresponding values in V to produce a single unique point. However, you can use groupsummary to 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.
%
% Use groupsummary to eliminate the duplicate sample points and preserve the maximum value in V at 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, groupsummary returns the unique groups P_unique as 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, scatteredInterpolant does not throw a warning.

I = scatteredInterpolant(P_unique,V_unique);

%
% Copyright 2020 The MathWorks, Inc.