'Interp1 error. Value X should be distinct.'

5 Ansichten (letzte 30 Tage)
Yun Inn
Yun Inn am 15 Jan. 2013
Bearbeitet: zohar am 4 Jun. 2015
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?

Antworten (2)

Jan
Jan am 15 Jan. 2013
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 Kommentar
zohar
zohar am 4 Jun. 2015
Bearbeitet: zohar am 4 Jun. 2015
+1 Just....
uniq = [true; diff(A1) ~= 0];

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 15 Jan. 2013
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 Kommentare
Yun Inn
Yun Inn am 15 Jan. 2013
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 am 15 Jan. 2013
Bearbeitet: Andrei Bobrov am 15 Jan. 2013
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');

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by