'Interp1 error. Value X should be distinct.'

5 vues (au cours des 30 derniers jours)
Yun Inn
Yun Inn le 15 Jan 2013
Modifié(e) : zohar le 4 Juin 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?

Réponses (2)

Jan
Jan le 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 commentaire
zohar
zohar le 4 Juin 2015
Modifié(e) : zohar le 4 Juin 2015
+1 Just....
uniq = [true; diff(A1) ~= 0];

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 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 commentaires
Yun Inn
Yun Inn le 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 le 15 Jan 2013
Modifié(e) : Andrei Bobrov le 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');

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by