Interpolating one variable to another with duplicate data points using interp1 - Error: Sample points must be unique.

53 views (last 30 days)
I am trying to interpolate data based on another variable, to 360 data points long. So for every increase of 1 degree in the pr_transpose variable, we can see what the sr_transpose variable is for 360 degrees. I have written the following code, which works great most of the time:
xq = 0:360;
for n = 1:size(pr_transpose, 1)
p{n} = pr_transpose(n, :);
s{n} = sr_transpose(n, :);
% Remove NaN values from the input arrays
validIndices = ~isnan(p{n}) & ~isnan(s{n});
% Perform interpolation only using non-NaN values
sr{n} = interp1(p{n}(validIndices), s{n}(validIndices), xq);
end
% Concatenate interpolated data from all rows
s_data = vertcat(sr{:});
However, I have a couple of trials of this dataset which feature a duplicate number! Which the interp1 function does not like as it requires that the data points to be unique. The following error message is shown:
Error using matlab.internal.math.interp1
Sample points must be unique.
I have the issue that I cannot alter the data by removing this point, so I think that I may need to change my approach. I identified the repeated number in the attached example data as being pr_transpose(1,17) by using the following:
[v, w] = unique( (pr_transpose(1,:)), 'stable' );
duplicate_indices = setdiff( 1:numel(pr_transpose(1,:)), w )
I have tried to use the resample function but I could not seem to get it to work. I don't think I can use the interp1 function without altering the data and removing the data point (which I can't do). I am probably just not approaching this from the right angle, so I would appreciate some input! Please let me know if you need any further information. Thank you!

Accepted Answer

John D'Errico
John D'Errico on 29 Jan 2024
Interpolation REQUIRES the data is unique in x. If there are two points with the same x value, then if y is not the same at those two points, it cannot choose between them, because interpolation is not alllowed to change the data.
Why cannot you remove the duplicates? It is not that you cannot, but that you do not know how to do so. In fact, it is pretty easy to do. It is not a problem for resample. You can download my consolidator utility from the file exchange and use that. It returns the average value for y at duplicated points.
Or you could always use the first of duplicated points. For example, here is a simple list of (x,y) pairs.
x = [0 1 1 2 3 3 3 4 5 5 5 5];
y = [0 1 2 3 4 5 6 7 8 9 10 11]; % being lazy here.
% use the average for y for each dup
[xu1,yu1] = consolidator(x,y)
xu1 = 6×1
0 1 2 3 4 5
yu1 = 6×1
0 1.5000 3.0000 5.0000 7.0000 9.5000
You can see it has averaged the y values at each unique value of x. I could have done the same using other tools.
% choose the first element of dups
[xu2,i,j] = unique(x);
yu2 = y(i)
yu = 1×6
0 1 3 4 7 8
Now xu and yu are pairs with unique x elements.
[xu;yu]
ans = 2×6
0 1 2 3 4 5 0 1 3 4 7 8

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!