Why does "interp1" result in an error "the grid vectors must contain unique points"?
148 views (last 30 days)
Show older comments
MathWorks Support Team
on 22 May 2019
Answered: MathWorks Support Team
on 12 Jun 2019
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
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!