'Interp1 error. Value X should be distinct.'

6 ビュー (過去 30 日間)
Yun Inn
Yun Inn 2013 年 1 月 15 日
編集済み: zohar 2015 年 6 月 4 日
I have matrix A below. When I try using interp1, I get this error: ??? Error using ==> interp1 at 259. The values of X should be distinct.
A= [17.4000,0.9949;
16.7000,0.9964;
15.9000,0.9978;
15.3000,0.9993;
14.5000,1.0000;
13.8000,1.0000;
13.1000,0.9985;
12.4000,0.9971;
11.7000,0.9942]
Val=interp1(A(:,1),A(:,2),15.0000)
Matlab treats 15.3 and 14.5 as 15? How do I get around this problem?

回答 (2 件)

Jan
Jan 2013 年 1 月 15 日
Finding duplicates is much cheaper, when you sort the data. Therefore I suggest to sort them explicitly, when you want to avoid the implicit sorting in unique():
[A1, index] = sort(A(:,1));
A2 = A(index, 2);
uniq = [true, diff(A1) ~= 0];
Value = interp1(A1(uniq), A2(uniq), 15.0000);
Btw. for a single value interp1 is not efficient. A hard-coded linear interpolation would be much cheaper. But this matters only, if this code section takes a significant part of the computing time.
  1 件のコメント
zohar
zohar 2015 年 6 月 4 日
編集済み: zohar 2015 年 6 月 4 日
+1 Just....
uniq = [true; diff(A1) ~= 0];

サインインしてコメントする。


Walter Roberson
Walter Roberson 2013 年 1 月 15 日
MATLAB R2012a outputs a value for me when I copy and paste your code.
However, if you were to alter your code slightly to
Val=interp1(A(:,2),A(:,1),15.0000)
then you would have a problem because A(:,2) contains two copies of 1.0000
Please recheck on your system.
  2 件のコメント
Yun Inn
Yun Inn 2013 年 1 月 15 日
Thank you. I found a duplicate somewhere else in the matrix and eliminated it with UNIQUE. However, UNIQUE sorted the data. Is there any way to eliminate duplicate without sorting?
Andrei Bobrov
Andrei Bobrov 2013 年 1 月 15 日
編集済み: Andrei Bobrov 2013 年 1 月 15 日
v = [2 5 6 5 8 3];
[a,b] = unique(v,'first');
ii = sort(b);
out = a(ii);
or
for R2012a and later
out = unique(v,'stable');

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by