How to fix error about interp1?

254 views (last 30 days)
SUNG GOOK YUN
SUNG GOOK YUN on 23 Oct 2022
Commented: Torsten on 23 Oct 2022
Hello,
I have some data and i want to run this code:
Pi=0:10:500;
pi=Pi';
WTEMP=interp1(wdepth(:,1),wtemp(:,1),pi);
It says Sample points must be unique.
wdepth is 273x1 double. / wtemp is 273x1 too.
wdepth have 0,30,50,100,150 values.
how can i fix it??
  1 Comment
Torsten
Torsten on 23 Oct 2022
Is "wdepth" sorted and has unique values ?
x = [0 0 2 3 1];
y = [1 2 3 4 5];
xq = 1.5;
yq = interp1(x,y,xq)
Error using matlab.internal.math.interp1
Sample points must be unique.

Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);

Sign in to comment.

Answers (2)

dpb
dpb on 23 Oct 2022
From the description it appears you have only 5 unique values in wdepth(:,1). As the doc and the error message say, interp1 can accept only unique values for the interpolating variable.
You'll have to pick which set of the unique values you want; one would presume there must be some other variables also in the total dataset (like a set of lat/long coordinates, maybe???) that led to there being multple values at the same value of the one variable.
Perhaps you could use interp2 or interpn instead with the full gridded array.

Star Strider
Star Strider on 23 Oct 2022
It would be helpful to have ‘wtemp’ and ‘wdepth’ to experiment with, as well as a description of what you want to do.
It may only be necessaary to reshape them to create a matrix from them. After that, there are matrix interpolation functions you can use (such as griddedInterpolant) to interpolate specific values.

Categories

Find more on Interpolation in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!