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
MathWorks Support Team 2019 年 6 月 13 日
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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by