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

14 次查看(过去 30 天)
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);

采纳的回答

MathWorks Support Team
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 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by