Why does "interp1" result in an error "the grid vectors must contain unique points"?

161 views (last 30 days)
Why does the following code return an error?
x= [1,2,3,4,1];
v= [1,1,1,1,1];
xq = [1 2 3 4 5 6 7 8 9 0 11 22 33 44 55 66 77 88 99 00 111 222 333 444 555 666 777 888 999 000];
vq = interp1(x',v',xq','linear','extrap')'
Error using griddedInterpolant
The grid vectors must contain unique points.
Error in interp1 (line 149)
F = griddedInterpolant(X,V,method);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Jun 2019
The error happens because "interp1" requires the values of "x" to be distinct.
>> unique_x=unique(x);
returns a 1x4 vector. Meaning there are duplicate values.
I suggest using the following instead:
[~, ind] = unique(x) % ind = index of first occurrence of a repeated value
vq = interp1(x(ind)',v(ind)',xq','linear','extrap')'
This will return the indices of the unique values in "x". Then you can call "interp1" on "x" and "v" as before, but this time pass the distinct element only.
There is one unique value in "vq" as expected since all the values in "v" are the same.

More Answers (0)

Tags

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!